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:

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:


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.

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.

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

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.


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 ():


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.

Add Comment

%d bloggers like this: