Can we use tuple as a key in dictionary always? Or there is any condition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Can we use tuple as a key in dictionary always? Or there is any condition

21st Sep 2018, 3:36 PM
Ishwarendra Jha
7 Answers
+ 2
You can't use tuples of mutable objects (for example tuples of dictionaries) as keys, because those are mutable.
21st Sep 2018, 5:33 PM
László Ozsvárt
László Ozsvárt - avatar
+ 6
If "hash(obj)" doesn't raise any errors you can use it as a key
21st Sep 2018, 4:00 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 3
Keys have to be immutable, otherwise they would make the whole concept of a dictionary senseless. So yes, tuples of any kind can work as keys no problem.
21st Sep 2018, 4:05 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
you can, please post a code for your try
21st Sep 2018, 3:59 PM
Sousou
Sousou - avatar
+ 2
Mert Yazıcı "hash(obj)" is a functions ?
21st Sep 2018, 4:01 PM
Sousou
Sousou - avatar
+ 2
s u can only when it is immutable
2nd Oct 2018, 3:41 AM
G.Anjali Jha
G.Anjali Jha - avatar