+ 2
Is that set can be key for the dictionary ?
I'm learning #python this was the quiz which was I founded in some python interview questions...... Can anyone tell answer for this? #sorry for my English
7 Antworten
+ 5
A dictionary key needs to be immutable. A set is not immutable. Hence, a set cannot be a key.
To find the answer, you can also try to create a dictionary with a set as key and see what happens.
https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-dicts/
+ 5
Just as addition:
The key must be immutable and hashable
The tuple (1,2,[3,4]) is immutable but not hashable and therefore not a key for dicts.
+ 3
Sobola Gabriel I don't quite understand what you mean. It is not about "what people say". When you are unsure, just test it in a code and see if it raises an error.
+ 1
Thanks Lisa
+ 1
This idea of the type of key for a dictionary is really confusing. Normally from the Python manual itself, Key should be immutable, but then you come across different materials on why/why not a set should be a key. Some will tell you that a set could be changed and others will say what is being taught here. So disturbing really.
0
Hi Lisa and Dineshkumar and Oma falk