List VS Tuple in Python – Differences Explained with Example


In this post we will learn about basic of list data-type in python, basic of tuple data-type in python and their difference with basic examples in place.

 

What is List in Python

List is a mutable and sequenced data type. List can contain heterogeneous values such as integers, floats, strings, tuples, lists, and dictionaries but they are commonly used to store collections of homogeneous objects. List containing other list as an element is called Nested list. List can be indexed and sliced.

There are several ways to create a list:

#Empty List
empty_list=[]

#List of similiar data types
list1=['a']
list2=['a','b','b']

#List comprehension
letters = [ letter for letter in 'Python' ]

#Type conversion
string="python"
list_converted=list(string)

Tuple in Python

A tuple is an immutable and sequential collection of values. Python tuple can store heterogeneous data types such as integers,floats, strings, lists, and dictionaries. As tuples are sequential data type they can also be indexed and sliced.

Tuples can be of different types:

# Different types of tuples

# Empty tuple
my_tuple = ()

# Tuple having integers
my_tuple = (1, 2, 3)

# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)

# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

 

List Vs Tuple – Difference

1) List are mutable and Tuples are immutable:

In programming, mutable objects (Changeable objects)are objects whose elements can be altered without recreating it. List’s elements can be updated, overloaded, removed, added, appended, extended etc. making it mutable.

#Mutability in list 

#adding elements in list 
l=[1,2,3] 
l.append(4) 
print(l) 
#output: [1,2,3,4] 
x=[5,6] 
l.append(x) 
print(l) 
#output: [1,2,3,4,[5,6]] 
l.extend(7) 
print(l) 
#output: [1,2,3,4,[5,6],7] 
y=[8,9] 
l.extend(y) 
print(l) 
#output: [1,2,3,4,[5,6],7,8,9] 
l.insert(0,0) 
print(l) 
#output: [0,1,2,3,4,[5,6],7,8,9] 

#Altering elements of list l[3]=0 
print(l) 
#output: [0,1,2,3,0,[5,6],7,8,9] 
l.reverse() 
print(l) 
#output: [9,8,7,[5,6],0,3,2,1,0] 
l2=[0,5,7,1,3,9,10,30,12,4,2] 
l2.sort() 
print(l2) 
#output: [0,1,2,3,4,5,7,9,10,12,30] 

#Removing elements from list: 
p=l.pop() 
print(l,p) 
#output: [0,1,2,3,0,[5,6],7,8] 9 
p=l.pop(4) 
print(p,l) 
#output: [5,6] [0,1,2,3,0,7,8,9] 
l.remove(0) 
print(l) 
#output: [1,2,3,0,7,8,9] 
l.clear() 
print(l) 
#output: []

Immutable means unchangeable. Tuples cannot add or modify elements once it is initialized. Like lists, tuples cannot be updated, overloaded, removed, appended, extended etc. making them immutable. Tuples does not have mutable methods like list.

#Tuples cannot be changed

t=(1,2,3)
t[0]=1
#Output: TypeError: 'tuple' object does not support item assignment
t.append(4)
#Output: AttributeError: 'tuple' object has no attribute 'append'
t.sort()
#Output: AttributeError: 'tuple' object has no attribute 'sort'
t.pop()
#Output: AttributeError: 'tuple' object has no attribute 'pop'
t.reverse()
#Output: AttributeError: 'tuple' object has no attribute 'reverse'

However by using ‘+=’ operator to append to a tuple.

#Using '+-' operator to append elements to a tuple
t=(1,2,3,4)
t+=(5,6)
print(t)
#Output: (1,2,3,4,5,6)

It worked because python interpreter created a new tuple with new element “appended” and assign it to its current variable. The old tuple is not changed, but is replaced!

 

2) Lists are unhashable while Tuples are hashable

An object is hashable if it has an integer which does not changes during its lifetime. Hash Values are used as dictionary keys to store and retrieve items really quickly from it. A hash function maps index keys to corresponding buckets in the hash index.

As lists are mutable they cannot be hashable objects. Mutable Objects canbnot be hashable because hashes are based on an object’s value and an object’s hash never changes. Thus only immutable objects can be hashable. So tuples immutability property makes them hashable.

#Hashing using list

l=[1,2]
d={}
d[l]=42
#Output: TypeError: unhashable type: 'list'

#Hashing using tuple

t=(1,2)
d={}
d[t]=42
print(d)
#Output: {(1, 2): 42}

 

3) List have more built in methods than tuple:

List has 46 built in methods while tuple has 33 built in methods. List and Tuple have 32 similar built in methods. Extra list built in methods are mutability methods  like append(), insert(), pop(), remove(), reverse() and sort() etc. Tuple has just 1 different built in method from list i.e. ‘__getnewargs__’ .

 

4) Elements of list are enclosed in square brackets [] and elements o f tuple are enclosed in parenthesis ():
print(type([1,2,3,4,5,6,7,8,9]))
#Output: <class 'list'>

print(type((1,2,3,4,5,6,7,8,9)))
#Output: <class 'tuple'>

 

Explore more Python Tutorials – Some basic information about python 

tags: list example in pythhon, list vs tuple differences, what is list, what is tuple, is list immutable, basic list program, tuple examples.

Leave a Comment

%d bloggers like this: