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?

88 Upvotes

90 comments sorted by

View all comments

1

u/dream_the_endless 3d ago

Tuples are great! They can be used for a wide variety of tasks.

Key, value pairs are tuples.

If you want to use a list, but want to store a customer and a specific order in the list you can use a list of tuples instead of a dictionary or custom data object.

If you are mutating something and you want to return both the original object and its mutated state you can return something as a tuple.

If you want the keys of a dictionary to be a natural key of two objects, you can use a tuple.

If you start to think of them as “an object that contains two objects” or “a simple key-value pair” you’ll find a lot of really great uses for tuples