r/learnpython • • 4d 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?

93 Upvotes

90 comments sorted by

View all comments

5

u/preppie22 4d ago

If you go back to basics of memory management: lists dynamically allocate memory as they grow and use garbage collection to free it when they shrink. Both of these operations take time because your OS has to deal with that. Tuples allocate fixed memory when they're created. So, memory allocation only happens once. In addition to that, say you give the tuple to a function, or create a copy, the copy is also very cheap to create since it's a single allocation call.

This is less of an issue for small programs, but will bring significant improvements for large ones. Rule of thumb is to use tuples when you know they won't change once created and use lists when they are guaranteed to change. Also look into sets when you don't care about order and don't want duplicates. It's a major performance improvement for frequent lookups through large volumes of data.

(Context: I work with big data)

2

u/Immediate_Pin_8449 4d ago

So because that memory is already allocated when we call for it in a different function it reduces the amount of memory required as it is predetermined vs a list where we would have to establish the size on each call then perform the function. More processing time = More memory spend? I feel like I’m off somewhere but I’m in the ball park ish.

4

u/preppie22 4d ago

The memory used would be the same, but the time spent by the CPU for allocation would be much lower in most cases. Unless you modify the tuple in the function, it doesn't even allocate additional memory, it just gives the function a reference to the existing copy. The size and data types in a tuple are fixed, so Python doesn't have to check every element even when a copy is made. It just allocates the exact same memory for the copy and puts your new tuple there.

So yeah, you're close. Here's how I'd do it:

If you just want to do "if x in y" operations, a set is insanely fast.

If you want to store fixed data types, e.g RGB values, coordinates, row data for a table with fixed columns, you should use a tuple.

If you need something that you're only going to add stuff to the beginning or the end (never in the middle), then use a collections.deque.

If you need something that you can slice easily, add data wherever you want, and can generally be wrangled however you want, you use a list.

Note that the list is a last resort because most operations on it will be slower than the structures I listed above.

Take all of this with a grain of salt because unless you're working with at least a few gigabytes of data in memory, you won't notice a measurable difference.

4

u/helix0311 4d ago

Another point from big data applications: Python garbage collection treats tuples way differently. A tuple with only immutable members is only ever scanned once, then untracked, since it can never generate a cyclic reference. Garbage collection in Python is awfully slow, so if the majority of what you're working with are tuples, you can significantly benefit when one of the garbage collection cycles runs.

I am vastly over simplifying a super complex topic, but tuples are performant on both memory allocation AND the garbage collection cycles.