What is the main difference between a Tuple and a List?

Asked

Viewed 10,708 times

15

What are the differences between a Tuple and a List in the Python?

Example:

>a = [1, 2, 3] 
# [1, 2, 3]
>b = (1, 2, 3)
# (1, 2, 3)

The Tuple, grotesquely speaking, it is a constant that accepts a List?

3 answers

16


From the technical point of view in Python a tuple is immutable and a list is mutable.

From the conceptual point of view you should use tuples to assemble heterogeneous data structures while the list should be used for homogeneous data, that is, all its elements should be of the same type.

Since Python is a dynamic language that cannot be guaranteed, it is up to the programmer to decide to do this.

Because it is used for heterogeneous data (diversity of types among members) the tuple usually has few elements but nothing prevents it from having many. Tuples are often used to simulate classes that need not be defined, whose use is more ephemeral and does not depend on more specific contracts. Just take care to use a tuple as if it were a list, it works, but is not usually suitable in almost every situation.

But even if you have a list of elements that will normally be small, if it looks more like a list and not a limited and fixed set of data, the list should be used.

If the data is of the same type, it is almost certain that you have a list, of course there are exceptions. For example a Point could be a tuple whose elements are two integers. This is obviously not a list of data, but a limited set of data that happens to be of the same type.

The tuple functions as a record of data, as a row of a database, a set of columns. The list works like the table, are the lines as a whole.

I imagine, but never tested, that tuples are slightly faster, but they should not be used because of this, use according to the proper semantics.

  • Good explanation! The question of the speed difference between tuples and lists always intrigued me too!

  • 1

    You could see the tuple as a set of data that doesn’t make sense in an uneven way. For example, a vector or coordinate, where the numbers immediately lose meaning if manipulated separately.

  • 1

    The speed of data access is the same. The difference in creation, if any, is minimal, and a minor detail of implementation.

  • Another interesting point is that we need fewer bits to store tuples. By using a tuple instead of a list we are saving space. If you are going to use a variable that needs to be modified along the code is better to use lists.

  • Missing example.

2

Complementing the user response Maniero, as tuplas sane immutable, therefore it is not possible modify the content without overwriting the variable that represents it, while the list has several methods that change its structure.

Where it is necessary to represent a structure that should not be modified, a tupla instead of lista, ensuring data integrity and ensuring that even the programmer cannot modify its structure.

For example:

t = (1, 2, 3)
t = t + (4, 5)  # incremento de 2 elementos na tupla t, sobrescrita na variável t
l = [1, 2, 3]
l.extend([4,5])  # incremento de 2 elementos na lista l
  • 1

    I left my -1 because the answer is not entirely true. It is a fact that the tuple is an immutable type, as also said in the other answer, but there is no relation to the fact that it represents structures that should not be modified. Even the fact that a tuple is immutable does not mean that its elements are immutable. For example, a list tuple can change freely because the list is changeable. See a example.

  • Yes, I ended up expressing myself badly. I was talking about the structure only of the tuple and not its content.

  • Hello! I would still like to better understand why tuples are most used for heterogeneous data types and lists for homogeneous data. This is not mandatory, but because it is more appropriate?

  • Generally, tuples are used to represent a set of data where each position has a well-defined value, such as in the @Niero response with the Point example, where each element of the tuple is a number, the first position being for x and the second being for y. In this example, the tuple is heterogeneous, but homogeneous elements can be used as well. Tuples is a representation of a data set with a defined quantity, while in the list one can increment elements.

2

Just complementing, another characteristic of tuples and lists is the addition of unit elements in each one. For example to add the element '4' in each one:

st = (1,2,3)  # Declaração de tupla
st = [1,2,3]  # Declaração de lista

st += [4]
sl += (4,)   #Sem a vírgula ele retorna um erro que não se pode concatenar um inteiro a uma tupla
sl += (4,5,6) # Para adições desse modo não há problema

Browser other questions tagged

You are not signed in. Login or sign up in order to post.