Using tuples as key in dict | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren

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 Antwort
+ 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:
9th Mar 2022, 7:50 AM
Seb TheS
Seb TheS - avatar