5 Major Difference Between List and Tuple in Python

Tuple and List are the very important data structures in Python to store the series of data. In this post, I am demonstrating the difference between list and tuple in Python.

If you are new Python programmer, let me tell you, it is collection object like an array in C, C++ programming. An array is far different from list and tuple. You will realize this at the end of this article.

In Python, having two data structures (List and Tuple) with a slight difference is more confusing for the python programmer. But in context to the use cases of both, they are distinct and both have their advantages and disadvantages.

Let’s start digging without wasting any time…

Difference between List and Tuple in Python:

I am trying to make it simple with examples for you.

1. Syntax

The syntax for list and tuple are slightly different.

Python List Example:

listWeekDays = ['mon', 'tue', 'wed', 2]

You can check the type of object created using type() function in Python.

type(listWeekDays)
#<class 'list'>

Python Tuple Example:

tupWeekDays = ('mon', 'tue', 'wed', 2)

Using type() function to check the tupWeekDays type.

type(tupWeekDays)
#<class 'tuple'>

If you look into the above code…

Tuple uses ( and ) to bind the elements where as list uses [ and ] to bind the elements in the collection.

You can even create tuple without ( and ) operators. Coma operator (,) is mendatory to distinguish elements in a tuple.

tupWeekDays = 'mon', 'tue', 'wed', 2

When you want to create empty tuple, you can not create it without ( and ) operators. The only ways are…

tupWeekDays = ()

tupWeekDays = tuple()

It’s very rare you will use empty tuple as it is immutable and you can not add elements once it is created.
If you don’t know what is an immutable data structure, here you go…

2. Mutability Vs Immutability

A tuple is a list which one can not edit once it is created in Python code. The tuple is an immutable data structure.

Whereas List is the mutable entity. You can alter list data anytime in your program.

Among all, this is the major difference between these two collections. I was asked in Druva interview questions, why tuple is immutable.

It is easy to demonstrate the mutability difference between tuple and list in Python with an example.

You can clearly see that, there are so many additional functionalities associated with a list over a tuple. You can do insert and pop operation, removing and sorting elements in the list with inbuilt functions.

In tuple, there is no index placed for each element. So you can not do the insert, pop, remove or sort elements. Moreover, it is immutable data type so there is no way to change the elements in a tuple.

4. Python List of Tuples:

How to create a list as a collection of tuples?

As you know everything in Python is an object (including the tuple and list). The list is a collection of object.

So you can create a list of tuple objects; code as given below.

listWeekDay=[('mon', 'tue', 'wed'),('sat','sun')]

In the above example, days in the list are separated as tuples, based on week days and week off days.

5. Where to use Tuple and List?

If you are writing a program where you need a collection of objects, you have two choices- Tuple vs List.

Sometimes in the program, you may need elements in the collection can be alerted any time. You must use a list instead of a tuple.

A tuple is immutable in Python and you can no change its elements once it is created. If you are creating collection and elements in the collection are not going to change, you must use Tuple.

In case if you tried to alter the tuple value, it will throw an error. So it prevents the value from changing.

Note: All the above-demonstrated examples are executed and tested on Python 2.7 and it will run on Python 3 as well. For every example, I have clear python interpreter console and executed to avoid the mess.

These are all the difference between list and tuple in Python. If you have any thought on these, write in a comment.

I am complete Python Nut, love Linux and vim as an editor. I hold Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack Portal.