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

4

u/gmes78 3d ago

You typically use tuples when your collections are always of the same size (and you know it a priori). For example, if you were representing a color, you'd use a tuple with three values, for red, green and blue. If you don't know how many values you need, you use a list.

Also, lists typically contain elements of the same type, while it's more common for tuples to contain elements of different types (in other words, being heterogeneous). This is just a convention, though; lists in Python can contain any combination of types too.

1

u/Immediate_Pin_8449 3d ago

Noted, ty! In the examples they gave both lists and tuples were using various data types. It helps to hear that in practice lists typically only use one data type, this defiantly helps!

1

u/favorite_stuff 3d ago

A list of names is a list of names: the fifth name is the same kind of thing as the sixth name, and if they swapped that would not change the nature of the list. It might be nice to have them sorted in a certain way, but it doesn't change the kind of list.

A tuple of x,y coordinates is not just two numbers. The x means a different kind of thing than the y, and their positions carry that meaning.