Register Login

Difference between list and tuple

Both List and Tuple are the two most popularly used compound data types in Python. They are popular because they provide a wide range of flexibilities, built-in functions, and methods. Both of them can store heterogeneous values and nested compound data types within themselves. In this article, you will understand the differences between these two compound data types.

Difference between list and tuple

LIST TUPLE
List is dynamic in nature Tuple is static in nature
List is a mutable data type; i.e., the list can be altered or transformed after its declaration and initialization according to programmer requirements. Tuple is an immutable data type; i.e., the tuple value or structure can't be altered or transformed after its declaration and initialization.
Implementing the iteration on each element is time-consuming in List. Iterating through each element of the tuple data is not time-consuming, and programmers can appropriately access each element of it without eating up the time.
The list has more space complexity as compared to tuple because it occupies more memory. Tuple eats up reasonable memory as compared to lists.
The collection of built-in methods and functions in the case of the list is much as compared to the tuple. Tuple has lesser built-in methods and functions as compared to lists.
Programmers dealing with lists are more likely to expect errors and unexpected changes. Programmers dealing with tuples do not have to encounter unexpected errors and alterations because Tuple internally restricts changes due to its immutable nature.
Handling list object is slower compared to tuple Handling tuple object is faster as compared to list
List is the most appropriate compound data type for insertion and deletion Tuple is best suitable for read-only operations like storing data and accessing data elements
Lists are of variable length Tuples are of fixed length

Size Comparison Program:

Tuples and lists differ in their size even though they store the same number of elements within it. This makes the tuples little bit faster than the lists. But in case of a very large collection of elements, this minute difference becomes negligible.

Program:

tup=(1,2,3,4,5,6)
li=[1,2,3,4,5,6]
print('Tuple size is:',tup.__sizeof__())
print('List size is:',li.__sizeof__())

Output:

Tuple size is: 72
List size is: 88



Other Critical use cases:

  1. If you use a tuple instead of a list, it gives the interpreter a hint that the data will be static and won't change during the program exection.
  2. To convert a list to a tuple, we use the tuple(). Similarly, to convert a tuple into a list, we use the list().

Program:

tup=(1,2,3,4,5,6)
li=[1,2,3,4,5,6]
a=list(tup)
b=tuple(li)

print(a)
print(b)

Output:

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

Conclusion:

Both Tuple and List are very useful in handling large set of data within a Python program. It is up to the programmer that he/she will decide which compound data type the programmer wants to use. Both have their own advantages and disadvantages.