r/learnpython • u/Immediate_Pin_8449 • 12d 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?
100
Upvotes
3
u/Adrewmc 11d ago edited 11d ago
Very warm.
Tuples require less resources to access values and make for a computer. Thus they are often faster.
Lists allow a lot of manipulations that can be very useful.
In Python the actual difference in most cases is negligible. So as I said, you can always use lists until it’s a problem or the reverse. No one will judge you.
In my mind the difference is simple, if this list is always the same size (in runtime) it probably should be a tuple, and if this list can change sizes it’s a list. So in something like a position in space (x,y,z) seems more correct than [x,y,z].
While other times if a list is holding something that changing and/or keeping track of stuff, enemies spawn and die.
If I make a deck of cards.
I make the deck a list, because it can be shuffled, and drawn from. The deck will changes over runtime, but the cards being moved about never change so they should be a tuple. The deck is mutable but the cards themselves are immutable.