We all know that Python is easy but a few of these data structures are completely different. Two of these are lists and tuples. Both are kind of similar but with certain key differences between them. In this article, we will give a detailed comparison of List vs Tuple in Python with examples.
Lists & Tuples in Python
Lists and tuples are both built-in data structures in python that allow us to store a collection of elements.
A list is ordered and mutable, meaning the values stored in a list can be appended, removed, and modified after its creation. They are defined using square brackets, ‘[__]', and are usually used for storing collections of items that may change over time. Lists can contain any data type of items, strings, integers, double values, and so on.
Here’s an example of the creation of a list:
example= [1, 2, 3, 4, 5]
Tuples are kind of similar in that they are also ordered and can contain any data type of values, char, int, double, etc., but tuples are immutable. This means that once a tuple is created, it cannot be modified in any manner. Any change, like any addition of new items, removal of an item, etc., will result in the creation of a new tuple.
The original tuple cannot be modified. Tuples are created using parenthesis or round brackets ‘(__)’ and are often used for representing fixed collections of related items.
Here’s an example of the creation of a tuple:
numbers = (1, 2, 3, 4, 5)
Are tuples faster than lists? Tuples are faster and require less memory to store than lists. This is because tuples are immutable, there is no need to change them after being created. So, when a tuple is created, Python only allocates a single contiguous block of memory. This also makes it easier to access elements.
Difference Between List and Tuple in Python
Here are some of the key differences between Lists and Tuples of Python.:
- Mutability: Lists are mutable, meaning the contents stored in a list can be changed or modified as per need. Tuples are immutable, their contents cannot be changed once defined.
- Performance: Tuples are faster for large collections of data. This is because, tuples are stored as a single unbroken block of data, while lists are stored as separate blocks.
- Syntax: Lists are defined using square brackets, like ‘[__]’. Tuples are created using parenthesis, like ‘(__)’.
- Memory Use: Tuples are stored as a single unbroken block of data, they tuples are quicker to access data and use lesser storage as compared to lists. Python also optimize tuples for their memory usage.
- Methods: Lists have access to more methods than tuples.
- Dictionaries: Tuples can be used as keys for dictionaries whereas Lists cannot.
Let's take a look at some examples to better see the differences between lists and tuples in Python:
Example 1) Creating and Modifying a List
my_list= [1, 2, 3, 4, 5] my_list[2] = 10 print(my_list)
Output:
[1, 2, 10, 4, 5]
In this example, we create a list my_list with five integers. We then modify the third item in the list (which has index 2) to be 10. Finally, we print the updated list and that will give an output of [1, 2, 10, 4, 5].
Example 2) Creating and Accessing a Tuple
numbers= (1, 2, 3, 4, 5) second_item = my_tuple[1] print(second_item)
Output:
2
In this example, we create a tuple my_tuple with five integers. We then access the second item in the tuple (which has index 1) and store it in a variable called second_item. Finally, we print the value of second_item which is 2.
Example 3) Using a Tuple as a Key in a Dictionary
my_dict = {(1, 2): 'value'} value = my_dict[(1, 2)] print(value)
Output:
'value'
In this example, we create a dictionary my_dict with a single key-value pair, where the key is a tuple (1, 2) and the value is the string 'value'. We then access the value associated with the key (1, 2) and store it
Example 4) What happens if a programmer tries to modify a list and a tuple:
my_tuple = (1, 2, 3, 4, 5) my_tuple[0] = 6
As shown above, when we tried to modify the first element of the tuple, we got a TypeError: 'tuple' object does not support item assignment.
Why Use a Tuple Instead of a List?
As we have seen above, both lists and tuples have their own pros and cons. There might be situations where a user would prefer a list and others where the user prefers to use tuples.
Tuples would be advantageous in avoiding situations where the contents of the sequence might accidentally get modified because they are immutable. Tuples are significantly faster and optimized for storage for accessing their elements.
But there is no single answer for which one is better between tuples and lists. If a programmer or user needs a sequence or data structure that can be changed over time, a list is more viable. Whereas if they would rather lock down the sequence, tuples are preferable.
Generally, lists are used more often, just because of their flexibility and ease of use. They can be modified, and since humans can be fickle at times, we use lists a lot.
Conclusion
So far, we have seen the various differences between two of Python’s data structures Lists and Tuples. Ultimately while deciding which data structure to use, the user must consider the performance needed, available memory, and the mutability of the collection they wish to create.