Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
List
3.
Tuple
4.
Comparison
5.
Lists vs. Tuples
6.
Frequently Asked Questions
6.1.
Which is faster, tuple or list?
6.2.
Is it possible to store the list in a tuple or vice-versa?
6.3.
Is it possible to modify the list stored in a tuple?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lists vs Tuples

Author Malay Gain
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

List and Tuple are the most commonly used data structure in the Python programming language. As these two data structures are pretty much similar, it is not really an easy task for beginners to differentiate their use cases. In this article, we will be mainly focusing on the key differences between lists and tuples by analyzing their properties and use cases.

Let’s start with a little introduction to the list and tuple.

Are You Ready Reaction GIF by Denyse®

Source: Giphy

List

The list is one type of mutable container that can store different types of data elements and iterate over the elements. It can be considered as one type of dynamic array in C.

Example

eg_list=[1,2,'Python',4]
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Tuple

A tuple is a collection of Python objects separated by commas, and it is static in nature.

It can also store different data types at the same time.

Example

eg_tuple=(4,5,'tuple',4)

Comparison

Syntactical difference 

From the above examples, we can easily identify that a list is surrounded by brackets [ ], whereas a tuple is surrounded by parenthesis ( ).

Modification

A list is mutable, i.e., the content of a list can be modified after its creation. But tuple can not be modified after its creation.

eg_list=[1,2,'Python',4]
eg_list[1]='new'
print(eg_list)

output 
[1, 'new', 'Python', 4]
eg_tuple=(4,5,'tuple',4)
eg_tuple[1]='new'
print(eg_tuple)

Output

TypeError: 'tuple' object does not support item assignment

Memory efficiency

As lists are mutable, when we need to extend the size of the list, extra allocation of memory is required. But as a tuple is immutable, the minimum memory block is allocated for it. In that context, the tuple is more memory efficient than lists.

eg_list=[1,2,'Python',4]
eg_tuple=(4,5,'tuple',4)
print('size of list=',eg_list.__sizeof__())
print('size of tuple=',eg_tuple.__sizeof__())

Output

size of list= 72
size of tuple= 56

 

You can practice by yourself with the help of online python compiler for better understanding.

Use cases

Lists are mainly used when data or the size of the container can be changed during runtime. But tuple is only used to store static data set. The list can not be used as the key in dictionary data structure due to its mutable property, but a tuple can be used as a key.

Built-in methods

Many built-in methods like insert, pop, sort or remove, and reverse are associated with the list. But such inbuilt functions are not available for tuples.

## find associated functions for each types
dir(eg_list)
dir(eg_tuple)

Lists vs. Tuples

Lists v/s Tuples

Check out this article - Balanced Parentheses

Frequently Asked Questions

Which is faster, tuple or list?

A tuple is faster than a list as for a tuple, memory is allocated from a single memory block, but for a list, memory is allocated from different chunks which are linked.

Is it possible to store the list in a tuple or vice-versa?

Yes, it is possible to store lists as elements of a tuple and vice versa.
 

Is it possible to modify the list stored in a tuple?

If a tuple contains a single list, it can’t be modified. But if there are multiple inner lists, then inner lists can be modified separately.

Example

Conclusion

This article covered the comparison between lists and tuples by describing their properties through code snippets.

Also, refer to the other two data structures of python, i.e., Dictionary and Sets.

Also check out - Data Dictionary In Software Engineering

Check out and enroll in our python courses to master the skills in the python programming language. Side by side, you can also practice a wide variety of  Data Structures and Algorithms questions commonly asked in interviews in Coding Ninjas Studio. Along with the practice of the problems, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! Also, you can find the interview experience of scholars working in renowned product-based companies here. 

Swipe Up Hello World GIF by SheCodes
Live masterclass