Difference Between List and Tuple in Python
Lists and Tuples in Python are two classes of Python Data Structures. The list structure is dynamic, and readily changed whereas the tuple structure is static and cannot be changed. This means that the tuple is generally faster than the list. Lists are denoted by square brackets and tuples are denoted with parenthesis.
Differences between List and Tuple in Python
Sno
| LIST
| TUPLE
|
---|
1 | Lists are mutable | Tuples are immutable |
2 | The implication of iterations is Time-consuming | The implication of iterations is comparatively Faster |
3 | The list is better for performing operations, such as insertion and deletion. | A Tuple data type is appropriate for accessing the elements |
4 | Lists consume more memory | Tuple consumes less memory as compared to the list |
5 | Lists have several built-in methods | Tuple does not have many built-in methods. |
6 | Unexpected changes and errors are more likely to occur | Because tuples don’t change they are far less error-prone. |
Python List vs Python Tuple
Test whether tuples are immutable and lists are mutable
Here we are going to compare the list and tuple mutability tests.
Python3
# Creating a List with
# the use of Numbers
# code to test that tuples are mutable
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("Original list ", List)
List[3] = 77
print("Example to show mutability ", List)
Output:
Original list [1, 2, 4, 4, 3, 3, 3, 6, 5]
Example to show mutability [1, 2, 4, 77, 3, 3, 3, 6, 5]
We can see here tuple can not be modified.
Python3
# code to test that tuples are immutable
tuple1 = (0, 1, 2, 3)
tuple1[0] = 4
print(tuple1)
Output:
Traceback (most recent call last):
File "e0eaddff843a8695575daec34506f126.py", line 3, in
tuple1[0]=4
TypeError: 'tuple' object does not support item assignment
Which is better list or tuple in Python?
To put this answer to the test, let’s run some operations on a Python Tuple and a Python List. This will give us a better idea of which is a better list or tuple in Python.
Test whether Tuples are Memory Efficient
As tuples are stored in a single memory block therefore they don’t require extra space for new objects whereas the lists are allocated in two blocks, first the fixed one with all the Python object information and second a variable-sized block for the data.
Python3
import sys
a_list = []
a_tuple = ()
a_list = ["Geeks", "For", "Geeks"]
a_tuple = ("Geeks", "For", "Geeks")
print(sys.getsizeof(a_list))
print(sys.getsizeof(a_tuple))
Output:
120
64
Test whether the implication of iterations is comparatively faster in Tuples
As tuples are stored in a single memory block, therefore, they don’t require extra space for new objects as they are immutable whereas the lists are allocated in two blocks, first the fixed one with all the Python object information and second a variable-sized block for the data which makes them even faster.
Python3
import sys, platform
import time
l=list(range(100000001))
t=tuple(range(100000001))
start = time.time_ns()
for i in range(len(t)):
a = t[i]
end = time.time_ns()
print("Total lookup time for Tuple: ", end - start)
start = time.time_ns()
for i in range(len(l)):
a = l[i]
end = time.time_ns()
print("Total lookup time for LIST: ", end - start)
Output:
Total lookup time for Tuple: 7038208700
Total lookup time for LIST: 19646516700
Mutable List vs. Immutable 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.
Python Indexing
Both lists and tuples allow you to access individual elements using their index, starting from 0.
Python3
my_list = [1, 2, 3]
my_tuple = (4, 5, 6)
print(my_list[0]) # Output: 1
print(my_tuple[1]) # Output: 5
Output:
1
5
Python Slicing
Both lists and tuples allow you to extract a subset of elements using slicing.
Python3
my_list = [1, 2, 3, 4, 5]
my_tuple = (6, 7, 8, 9, 10)
print(my_list[1:3]) # Output: [2, 3]
print(my_tuple[:3]) # Output: (6, 7, 8)
Output :
[2, 3]
(6, 7, 8)
Python Concatenation
Both lists and tuples can be concatenated using the “+” operator.
Python3
list1 = [1, 2, 3]
list2 = [4, 5, 6]
tuple1 = (7, 8, 9)
tuple2 = (10, 11, 12)
print(list1 + list2) # Output: [1, 2, 3, 4, 5, 6]
print(tuple1 + tuple2) # Output: (7, 8, 9, 10, 11, 12)
Output :
[1, 2, 3, 4, 5, 6]
(7, 8, 9, 10, 11, 12)
Note – However, there are some operations that are available only for lists, due to their mutability.
Python Append
Lists can be appended with new elements using the append() method.
Python3
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]