Skip to content
Related Articles

Related Articles

Python | Difference Between List and Tuple

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 06 Dec, 2022
Improve Article
Save Article

List and Tuple in Python are the classes of Python Data Structures. The list is dynamic, whereas the tuple has static characteristics. This means that lists can be modified whereas tuples cannot be modified, the tuple is faster than the list because of static in nature. Lists are denoted by the square brackets but tuples are denoted as parenthesis.

Important differences between List and Tuple in Python

SR.NO. 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. 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 The unexpected changes and errors are more likely to occur In tuple, it is hard to take place.

 List vs Tuple

Test whether tuples are immutable and list are mutable

Here we are going to compare the list and tuple mutability test.

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

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 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 more 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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!