I was studying the difference between lists and tuples (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.
A sentence in the article got me:
Only immutable elements can be used as
dictionary keys, and hence only tuples
and not lists can be used as keys.
I have a hard time thinking of a situation where I would like to use a tuple as a dictionary key. Can you provide an example problem where this would be the natural, efficient, elegant, or obvious solution?
Edit:
Thanks for your examples. So far I take that a very important application is the caching of function values.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…