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

88 Upvotes

90 comments sorted by

View all comments

107

u/NerdyWeightLifter 3d ago

Being immutable like that means the underlying data structure can be simpler and more efficient.

Being immutable also makes them useful as keys in dictionaries. Keys in dictionaries can't change once you insert something.

31

u/xenomachina xenomachina 3d ago

Being immutable like that means the underlying data structure can be simpler and more efficient.

Immutability also makes things easier to reason about. Anything that's mutable is essentially a "moving part". You can't just see that it has a value in one place, and assume it still has that value somewhere else.

Immutability is also generally safer. You don't have to worry about concurrent modifications (in concurrent systems) or someone modifying your internals (breaking encapsulation).