Python

What is the difference between tuple and list in Python?

March 14, 2022

What is the difference between tuple and list in Python?

In this article we will learn the difference between tuple and list in Python, when using each of the approaches so that your code follows good practices.

What’s up programmer, ok? Let’s learn more about tuple and list data structures in Python.

The big difference we have between tuples and lists is that tuples are not mutable

That is, it is not possible to modify the content without rewriting the value that the variable represents

Lists can be changed normally and we have methods that help change their structure.

See the syntax of each of the data structures:

# tuple
myTuple = (1, 2, 3)

# list
myList = [1, 2, 3]

Another notable difference is in the syntax, the tuple is represented by ( ), 

whereas the list by [ ]

Data Types

Another important concept of the tuple is that it is designed for heterogeneous data, that is, of different types.

The list for homogeneous, of equal types

However, Python does not block this in any way, it is up to the programmer to respect it

As shown in the first example created, we use a tuple with only integers

Use

Lists are generally used to store multiple items and apply repetition structures such as for or while to them.

Tuples, on the other hand, represent a more fixed structure, where the idea is to use the way it is presented to us.

But only the structure should be kept as the data can change

Conclusion

In this article we learned the difference between tuple and list

The tuple structure is immutable, the list is mutable and can grow freely.

Both have different uses, as stated in the article, the list for example can be infinitely extended.

As for the tuple, in theory, we should keep its structure

Do you want to learn more about Python? Click here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x