Try it here
Subscribe
Python tuple and list

Difference between tuple and list

difference_between_tuple_and_list

Tuple

A tuple is a collection of values, and we declare it using parentheses. However, we can also use tuple packing to do the same, and unpacking to assign its values to a sequence of variables.

>>> numbers=(1,2,'three')
>>> number=4,5,6
>>> a,b,c=number
>>> print(numbers,a,b,c,number,type(numbers))
(1, 2, 'three') 4 5 6 (4, 5, 6) 
>>> 

To access a tuple, we use indexing, which begins at 0.

>>> number[0]
4
>>> 

We can also slice it to retrieve a part of it.

>>> number[:-1]
(4, 5)
>>> 

Finally, we can delete an entire tuple.

>>> del number
>>> number
Traceback (most recent call last):
  File "", line 1, in 
    number
NameError: name 'number' is not defined
>>> 

List

We create lists using square brackets.

>>> colors=['red','blue','green']
>>> 

We can slice lists too.

>>> colors[:-1]
['red', 'blue']
>>> 

We can reassign elements and delete can delete them

>>> colors
['black', 'blue', 'green']
>>> del colors[1]
>>> colors
['black', 'green']
>>> del colors
>>> colors
Traceback (most recent call last):
  File "", line 1, in 
    colors
NameError: name 'colors' is not defined
>>> 

Difference between list and tuple:

List is Mutable

We can reassign the elements and can reassign the entire list

>>> list1=[i for i in range(10)]
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list1[0]=10
>>> list1
[10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list1=[11,12,13]
>>> list1
[11, 12, 13]
>>> 

We can delete one element,delete a slice of the list and can reassign a slice

>>> del list1[1]
>>> list1
[11, 13]
>>> list1[0:2]=[1,2]
>>> list1
[1, 2]
>>> 

Tuple is Immutable

We cannot reassign in tuple but can reassign an entire tuple.

>>> tuple1[1]=10
Traceback (most recent call last):
  File "", line 1, in 
    tuple1[1]=10
TypeError: 'tuple' object does not support item assignment
>>> tuple1=6,7,8
>>> tuple1
(6, 7, 8)
>>> 

We can slice tuple to access or but cannot delete it.

>>> tuple1[:2]
(6, 7)
>>> del tuple1[:2]
Traceback (most recent call last):
  File "", line 1, in 
    del tuple1[:2]
TypeError: 'tuple' object does not support item deletion
>>> 

As is visible, we can slice it to access it, but we can’t delete a slice. This is because it is immutable.

We cannot delete a single element but can delete entire tuple

>>> del tuple1[0]
Traceback (most recent call last):
  File "", line 1, in 
    del tuple1[0]
TypeError: 'tuple' object doesn't support item deletion
>>> del tuple1
>>> tuple1
Traceback (most recent call last):
  File "", line 1, in 
    tuple1
NameError: name 'tuple1' is not defined
>>> 

In summary :-

Tuples are fixed size in nature whereas lists are dynamic.

In other words, a tuple is immutable whereas a list is mutable.

  1. You can't add elements to a tuple. Tuples have no append or extend method.
  2. You can't remove elements from a tuple. Tuples have no remove or pop method.
  3. You can find elements in a tuple, since this doesn’t change the tuple.
  4. You can also use the in operator to check if an element exists in the tuple.

Tuples are faster than lists. If you're defining a constant set of values and all you're ever going to do with it is iterate through it, use a tuple instead of a list.

Tuples are stored in a single block of memory. Tuples are immutable so, It doesn't require extra space to store new objects.

Lists are allocated in two blocks: the fixed one with all the Python object information and a variable sized block for the data.

It is the reason creating a tuple is faster than List. It also explains the slight difference in indexing speed is faster than lists, because in tuples for indexing it follows fewer pointers.

Also, you can't use a list as a key for a dictionary. This is because only immutable values can be hashed. Hence, we can only set immutable values like tuples as keys.

Writer profile pic

Anmol on Oct 04, 2020 at 11:04 am


This article is contributed by Anmol. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.