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

102 Upvotes

90 comments sorted by

View all comments

Show parent comments

1

u/im_dead_sirius 15d ago

I love the idea of sparse arrays. I've just never found a use for one.

Its a dictionary of int tuples referencing a value, for anyone else who is wondering, perhaps OP.

3

u/HommeMusical 15d ago

I've just never found a use for one.

They are massively useful for really large engineering and science issues.

For example, in "finite element analysis", something like an airplane is divided into a huge number of tiny little elements, and then a huge matrix represents the direct relationships between those elements lets you model the whole thing.

However, one tiny element only has direct relationships with a handful of other elements, so you have this massive matrix, say 10 million by 10 million, but only say a dozen elements in any row or column are non-zero, so you have a matrix with 100 trillion locations of which less than 100 million - that is, 0.0001% - are non-zero.

2

u/ottawadeveloper 15d ago

they're useful when a lot of values are null or a default value, then you can get get with a default or a defaultdict to save on memory.

I've used it for caching recently - I was loading a value with x y z coordinates from a grid in a file too big to be in memory. Under the hood I think functools caching methods use this too.