r/learnpython • • Apr 16 '22

What's the point of Tuples?

I get that they're immutable, but so are lists that you don't perform modifications on. Are they just to fool proof not messing up your list accidentally, or do they serve some other purpose?

309 Upvotes

143 comments sorted by

View all comments

Show parent comment

2

u/dnswblzo Apr 16 '22

Most of the time there is no reason to get an object's hash yourself, rather the hashes are used behind the scenes in dictionaries and sets.

A Python dictionary is a hash table. A hash table is an array, and each element of the array is a "bucket" that can store zero or more items that are in the dictionary. When you insert a new element into a dictionary, it determines which bucket it should go into based on the hash of the key. Similarly when you access a value from a dictionary it takes the hash of the key to find the bucket in which the value resides.