Difference Between List and Tuple in Python
Last Updated : 13 Mar, 2025
In Python, lists and tuples both store collections of data, but differ in mutability, performance and memory usage. Lists are mutable, allowing modifications, while tuples are immutable. Choosing between them depends on whether you need to modify the data or prioritize performance and memory efficiency.
Key Differences between List and Tuple
S.No | List | Tuple |
---|
1 | Lists are mutable(can be modified). | Tuples are immutable(cannot be modified). |
2 | Iteration over lists is time-consuming. | Iterations over tuple is faster |
3 | Lists are better for performing operations, such as insertion and deletion. | Tuples are more suitable for accessing elements efficiently. |
4 | Lists consume more memory. | Tuples consumes less memory |
5 | Lists have several built-in methods. | Tuples have fewer built-in methods. |
6 | Lists are more prone to unexpected changes and errors. | Tuples, being immutable are less error prone. |
Mutability Test: List vs Tuples
List are Mutable
Lists can be modified, meaning their elements can be changed, added or removed after creation.
Python a = [1, 2, 4, 4, 3, 3, 3, 6, 5] # Modifying an element in the list `a` a[3] = 77 print(a)
Output[1, 2, 4, 77, 3, 3, 3, 6, 5]
Explanation: Here, we modified the fourth element (index 3) from 4 to 77. Lists allow direct modification of their elements.
Tuples are Immutable
Tuples cannot be modified after creation. Any attempt to change an element will result in an error.
Python b = (0, 1, 2, 3) # Attempting to modify a tuple b[0] = 4 print(b)
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 4, in <module>
b[0] = 4
~^^^
TypeError: 'tuple' object does not support item assignment
Explanation: Tuples do not support item assignment, making them immutable. This prevents unintended modifications.
Performance Comparison: Lists vs Tuples
Memory Efficiency Test
Since tuples are stored in a single memory block and do not require additional space for dynamic changes, they are more memory-efficient than lists.
Python import sys # Creating an empty list and tuple a = [] b = () # Assigning values a = ["Geeks", "For", "Geeks"] b = ("Geeks", "For", "Geeks") # Checking memory usage print(sys.getsizeof(a)) print(sys.getsizeof(b))
Explanation: This code shows that the list takes more memory than the tuple. Tuples, being immutable, require less overhead, making them a better choice for storing fixed data.
Iteration Speed Test
Tuples have a performance advantage in iterations because they are immutable and stored in a contiguous memory block, reducing overhead.
Python import time # Creating a large list and tuple a = list(range(100000001)) b = tuple(range(100000001)) # Timing tuple iteration start = time.time_ns() for i in range(len(b)): x = b[i] end = time.time_ns() print(end - start) # Timing list iteration start = time.time_ns() for i in range(len(a)): x = a[i] end = time.time_ns() print(end - start)
Output
16649292352
12447795073
Explanation: Tuples iterate faster than lists as they are stored in a single memory block, while lists need extra operations for dynamic resizing.
Operations - Lists vs Tuples
In Python, both lists and tuples support a range of operations, including indexing, slicing, concatenation, and more. However, there are some differences between the operations that are available for lists and tuples due to their mutability and immutability, respectively.
Indexing
Both lists and tuples allow you to access individual elements using their index, starting from 0.
Python a = [1, 2, 3] # list b = (4, 5, 6) # tuple print(a[0]) print(b[1])
Slicing
Both lists and tuples allow you to extract a subset of elements using slicing.
Python a = [1, 2, 3, 4, 5] b = (6, 7, 8, 9, 10) print(a[1:3]) print(b[:3])
Concatenation
Both lists and tuples can be concatenated using the "+" operator.
Python # List Concatenation a = [1, 2, 3] b = [4, 5, 6] print(a + b) # Tuple Concatenation a = (7, 8, 9) b = (10, 11, 12) print(a + b)
Output[1, 2, 3, 4, 5, 6] (7, 8, 9, 10, 11, 12)
List-Specific operations
Only lists support operations that modify their contents:
- Append: Adds an element at the end.
- Extend: Merges another list.
- Remove: Removes the first occurrence of a value.
Python a = [1, 2, 3] a.append(4) a.extend([5, 6]) a.remove(2) print(a)
Explanation: append(4) method adds the number 4 to the end of the list, making it [1, 2, 3, 4]. Next, the extend([5, 6]) method adds multiple elements [5, 6] to the list, updating it to [1, 2, 3, 4, 5, 6]. The remove(2) method then deletes the first occurrence of 2 from the list, resulting in [1, 3, 4, 5, 6]
When to Use Tuples Over Lists?
In Python, tuples and lists are both used to store collections of data, but they have some important differences. Here are some situations where you might want to use tuples instead of lists:
- Immutability: Tuples are immutable, ensuring data integrity by preventing modifications. Ideal for constants, configurations, and fixed data.
- Performance: Tuples are faster and more memory-efficient than lists as they are stored in a single block and don’t require extra space for modifications.
- Memory Efficiency: Tuples use contiguous memory, while lists need extra space for dynamic resizing, making tuples a better choice for large, unchanging datasets.
Similar Reads
Difference between List and Array in Python In Python, lists and arrays are the data structures that are used to store multiple items. They both support the indexing of elements to access them, slicing, and iterating over the elements. In this article, we will see the difference between the two.Operations Difference in Lists and ArraysAccessi
6 min read
Difference between List and Dictionary in Python Lists and Dictionaries in Python are inbuilt data structures that are used to store data. Lists are linear in nature whereas dictionaries stored the data in key-value pairs. In this article, we will see the difference between the two and find out the time complexities and space complexities which ar
6 min read
Difference between for loop and while loop in Python In this article, we will learn about the difference between for loop and a while loop in Python. In Python, there are two types of loops available which are 'for loop' and 'while loop'. The loop is a set of statements that are used to execute a set of statements more than one time. For example, if w
4 min read
Create a List of Tuples in Python The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
3 min read
Python - Accessing nth element from tuples in list We are given tuples in list we need to access Nth elements from tuples. For example, we are having a list l = [(1, 2), (3, 4), (5, 6)] we need to access the Nth element from tuple suppose we need to access n=1 or 1st element from every tuple present inside list so that in this case the output should
2 min read