In Python, tuples and lists are similar data structures apart from mutability. This article discusses Python tuple vs list to compare the syntax, definition, mutability, and performance of both data structures.
Tuple vs List Definition
A Python list is a mutable data structure that we use to store elements in a sequential manner. We use a list to store data when we need random access to the elements.
A tuple is an immutable data structure that we use to store elements. A tuple in Python has all the properties of a list except mutability.
Tuple vs List Syntax
Tuple and List have different syntaxes. We use square brackets to define a list. On the other hand, parentheses are used to define a tuple. You can observe this in the following example.
myList=[1,2,4,5,6,6,7,8,99,33,4324,2,672]
myTuple=(1,2,4,5,6,6,7,8,99,33,4324,2,672)
Similarly, we use the list() function to create a list whereas the tuple() function to create a tuple from an existing collection object such as a set. You can observe this in the following example.
mySet={1,2,3,4,5}
myList=list(mySet)
myTuple=tuple(mySet)
Tuple vs List Mutability
A python tuple is immutable whereas a list is mutable. If you try to change a tuple by any means, the program will run into an error.
- We cannot add an element to a Python tuple. On the other hand, we can add elements as well as other container objects to an existing list.
- We can delete elements from a list. On the contrary, we cannot delete any element from an existing Python tuple.
Python Tuple vs List Performance
If we traverse a tuple and a list, traversing a tuple is more efficient than traversing a list.
- The performance of a tuple in this case is at least 10 percent better than a Python list.
- A tuple has a fixed size. So, it contains occupies only the space needed for its elements. On the other hand, a list occupies almost double the space needed by its elements. This makes Python lists inefficient compared to tuples.
Considering the above points, we can say that a Python tuple has better performance than a list.
For example,
- Traversing a list containing 13 elements takes almost 209 ns ± 36 ns per loop when we do nothing except traverse the list.
- Traversing a Python tuple with the same number of elements takes 191 ns ± 16.3 ns which is a considerably lower time.
You can observe this in the following example.
When to Use Tuple vs List in Python?
If you don’t need to change the data in the sequence after defining it, you should use a Python tuple. If you need to add, update, or delete elements from a sequence, you should use a list in Python.
Conclusion
In this article, we had a discussion about tuple vs list in Python to have a look at the definition, syntax, and performance of both data structures. To learn more about Python programming, you can read this article on for loop vs while loop in Python. You might also like this article on if vs elif vs else if in Python.
I hope you enjoyed reading this article. Say tuned for more informative articles.
Happy learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.