Using tuples as key in dict | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Using tuples as key in dict

What is a practical application of why you would use a tuple (especially larger tuples) as a key in a dictionary?

9th Mar 2022, 5:33 AM
Julius Adams
1 Answer
+ 2
I don't know many purposes for using large tuples as dictionary key, but I may know one. If you have a slow function and a high likelihood to call the function multiple times with the same values it can be good idea to put the function arguments in tuple and use the tuple as a dictionary item key and the function return value as the dictionary item value. Then you will be able to use the dictionary as a function for the values you have already calculated. ... I don't really know where it would be useful, because having too many items in a dict can also cause problems. Anyways here is an example: https://code.sololearn.com/c4mnJTryt0my/?ref=app
9th Mar 2022, 7:50 AM
Seb TheS
Seb TheS - avatar