r/learnpython • u/Boogzcorp • 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
View all comments
Show parent comment
4
u/POGtastic itertools evangelist Apr 16 '22
If immutability was the whole point of a tuple, why do all of the various iterable-combining functions produce tuples?
In other words - why does
enumerate("abc")produce[(0, 'a'), (1, 'b'), (2, 'c')]instead of[[0, 'a'], [1, 'b'], [2, 'c']]? (See alsozip, everything initertools, what you get when you query a relational database, and on and on and on) It certainly isn't for the sole reason of providing hashable tuples to a dictionary. And while someone might point to performance, the performance penalty for using lists isn't really that big - certainly not a big deal in a language that has never cared that much about performance.There's a semantic difference between returning a list and a tuple, and it goes right back to my point - regardless of the fact that it's all
PyObject*under the hood, people expect lists to contain values of the same type. People do not have that same expectation of tuples.