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 ArraysAccess
6 min read
Difference between + and , Python Print
In this article, we will learn about the difference between + and, in Python print, The print() function in Python is used to print some messages as the output on the screen. We can print a single element or even multiple elements on the screen. Python provides two ways to print multiple elements as
3 min read
Python | Difference between two lists
The difference between two lists in Python refers to the elements that are present in one list but not in the other. For example, finding the difference between lists a = [1, 2, 3, 4] and b = [3, 4, 5, 6] can result in [1, 2] by removing the common elements (3 and 4). Using setSet operations are mos
3 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
Python - Difference between sorted() and sort()
Sorting means rearranging a given sequence of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of the elements in the respective data structure. For example, The below list of characters is sorted in increasing order of their ASCII
6 min read
Difference between Append, Extend and Insert in Python
In Python, append(), extend() and insert() are list methods used to add elements to a list. Each method has different behaviors and is used in different scenarios. AppendThe append() method adds a single element which can be string, integer, tuple or another list to the end of the list. If we pass a
3 min read
Difference between List comprehension and Lambda in Python
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp. A list comprehension generally consists of these parts : Output expression,Input sequence,A var
3 min read
What Is Difference Between Del, Remove and Pop on Python Lists?
del is a keyword and remove(), and pop() are in-built methods in Python. The purpose of these three is the same but the behavior is different. remove() method deletes values or objects from the list using value and del and pop() deletes values or objects from the list using an index. del Statementde
2 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
Convert Tuple to List in Python
In Python, tuples and lists are commonly used data structures, but they have different properties. Tuples are immutable, meaning their elements cannot be modified after creation, while lists are mutable, allowing for changes. Sometimes, it is necessary to convert a tuple into a list to manipulate it
3 min read