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 a new Python programmer, let me tell you, it is a 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.
This is one of the most common Python interview questions asked in many of the interviews.
Let’s start digging without wasting any time…
Table of Contents
I am trying to make it simple with examples for you.
The syntax for the 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 a list uses [
and ]
to bind the elements in the collection.
You can even create tuple without (
and )
operators. Comma operator (,
) is mandatory to distinguish elements in a tuple.
tupWeekDays = 'mon', 'tue', 'wed', 2
When you want to create an empty tuple, you can not create it without (
and )
operators. The only ways are…
tupWeekDays = ()
tupWeekDays = tuple()
It’s very rare you will use an 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…
A tuple is a list that one cannot 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.
Tuple:
>>> tupWeekDays = ('mon', 'tue', 'wed', 2) >>> print(tupWeekDays) ('mon', 'tue', 'wed', 2) >>> tupWeekDays(2)='fri' Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment
List:
>>> tupWeekDays = ['mon', 'tue', 'wed', 2] >>> print(listWeekDays) ['mon', 'tue', 'wed', 2] >>> listWeekDays[2]='fri' >>> print(listWeekDays) ['mon', 'tue', 'fri', 2]
For more clarification, you must read the difference between mutable and immutable. There, I have explained, why tuple is immutable with more examples in a better way.
There are more functions are associated with List than Tuple.
You can use a dir([object])
inbuilt function to get all the associated functions for list and tuple.
Simply create a tuple and list. Pass these objects to dir function. You get a list of the associated functions as below.
>>> tupWeekDay=('mon', 'mon', 'wed', 2) >>> dir(tupWeekDay) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__get slice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__ lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__' , '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count ', 'index'] >>> listWeekDay=['mon', 'mon', 'wed', 2] >>> dir(listWeekDay) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delsli ce__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getit em__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__r educe__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__' , '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'a ppend', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort' ]
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.
Some List Functionality examples you would like to give a try…
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 an immutable data type so there is no way to change the elements in a tuple.
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 objects.
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 weekdays and week off days.
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 that can be alerted at any time. You must use a list instead of a tuple.
You can also take keyboard input from the user and insert a new element in a List.
So, what is the advantage of using tuples over lists?
A tuple is immutable in Python and you can not change its elements once it is created. If you are creating collections 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 a 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 thoughts on these, write in a comment.
It is really an awesome post. I work as Python developer. You have cleared many of the doubts about list and tuple.
Keep it up your good work.
One more thing… Please update me if you post any Python article in future.
Thanks, Amrender! I am glad as you find this article useful to improve your Python skillset.
For updates, you can subscribe to our weekly newsletter.
Stay tuned!
Under 1. Syntax, Line 1 should read:
listWeekDays = [‘mon’, ‘tue’, ‘wed’, 2]
Hi Zack,
It was a typo mistake.
Updated. Thanks for correcting me!
An element can be added to an existing tuple, is this an exception to the immutability property of the tuple?
You can not modify the elements in the tuple. When you do the workaround to add a new element in the tuple, it might be creating a new tuple.
You can have look here – https://www.csestack.org/difference-between-mutable-and-immutable-in-python/#tuple to understand the immutable nature of tuple.
Hi, I am using Python 3.7 version. I have tried changing the list using list WeekDays(2)=’fri’. But, I am getting the error
can’t assign to function call.
Hi Udaysree, there was a mistake in our code. I have corrected it in the tutorial. To change the element in the list, you have to use a square bracket. listWeekDays[2]=’fri’ is the right way of doing this.
Thanks for the correction.
Well, why the tuples are immutable? Are there any applications of it?
Both data types serve a different purpose. We already have discussed this. Kindly check- Why is Tuple Immutable Data Type in Python?
Typo for “A tuple is immutable in Python and you can no change…” toward the end (5th last sentence). It should read “you cannot change…”
Thanks for the correction, SoheebAziz! Edited.