Saturday, November 16, 2024
Google search engine
HomeLanguagesDifference Between List and Tuple in Python

Difference Between List and Tuple in Python

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.

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 In a tuple, it is hard to take place.

 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]


Output :

[1,2,3,4]

Python Extend

Lists can also be extended with another list using the extend() method.

Python3




list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1) # Output: [1, 2, 3, 4, 5, 6]


Output :

[1, 2, 3, 4, 5, 6]

Python Remove

Lists can have elements removed using the remove() method. 

Python3




my_list = [1, 2, 3, 4]
my_list.remove(2)
print(my_list) # Output: [1, 3, 4]


Output :

[1, 3, 4]

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 – 

Immutable DataTuples are immutable, thus once they are generated, their contents cannot be changed. This makes tuples a suitable option for storing information that shouldn’t change, such as setup settings, constant values, or other information that should stay the same while your programme is running.

Performance Tuples are more lightweight than lists and might be quicker to generate, access, and iterate through since they are immutable. Using a tuple can be more effective than using a list if you have a huge collection of data that you need to store, retrieve, and use regularly and that data does not need to be altered.

Data integrity – By ensuring that the data’s structure and contents stay consistent, tuples can be utilised to ensure data integrity. To make sure the caller is aware of how much data to expect, for instance, if a function returns a set amount of values, you might want to return them as a tuple rather than a list.

RELATED ARTICLES

Most Popular

Recent Comments