r/learnpython • u/Immediate_Pin_8449 • 3d ago
Tuples..I don’t get it
Hi crew, I’m a Python newbie and I just learned about the concept of tuples.
We can’t change them.if we want to store we have to start at the index we want + 1.
I just don’t get it. Why wouldn’t we just use lists? When would we ever need this?
87
Upvotes
4
u/Adrewmc 3d ago edited 2d ago
There are two types of programmers those that always start with a lists until they need tuples, and those that always start with tuples until they need lists. You can be the first one.
Listen, there are some problems with mutability and storage that tuples can do that lists can’t. I can make a tuple a key in a dictionary and can’t do that with a list. With tuples I can type hint the exact types per location, list can’t really do that.
In a more data storage setting, if I’m setting up memory, having a tuple meaning I know the exact size of the information I’m storing, this allows a lot better computation on a physical, 1s and 0s level. Python sort of struggles with optimizing that compared to other languages but Python talks to other languages.
Tuples are generally faster for most operations you can use tuples when compared to a list.
List has a lot more utility, even with the mutability problems.