+ 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

30th Jul 2022, 5:07 PM
Dineshkumar R
Dineshkumar R - avatar
7 ответов
+ 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/
30th Jul 2022, 5:13 PM
Lisa
Lisa - avatar
+ 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.
30th Jul 2022, 7:18 PM
Oma Falk
Oma Falk - avatar
+ 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.
1st Aug 2022, 3:23 PM
Lisa
Lisa - avatar
+ 1
Thanks Lisa
30th Jul 2022, 5:20 PM
Dineshkumar R
Dineshkumar R - avatar
+ 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.
1st Aug 2022, 3:17 PM
Sobola Gabriel
Sobola Gabriel - avatar
0
Hi Lisa and Dineshkumar and Oma falk
31st Jul 2022, 8:53 PM
Hapsat Yunusa
0
Lisa but then proper understanding of Python literals is very vital to unravel this issues.
1st Aug 2022, 3:46 PM
Sobola Gabriel
Sobola Gabriel - avatar