All Courses

Tuple vs List: Difference Between List and Tuple in Python

Pallavi Dhotre

2 months ago

Tuple vs List in Python | insideAIML
Table of Content
  • Introduction
  • Tuple vs List in Python
  • Difference between List and Tuple in Python with example
  • Summary

Introduction 

          In a different article of the same series, we have discussed Lists and Tuples. Both the terms have the same purpose of storing the data with a different nature. Then the question that arises here is what is the difference between list and tuple in python? And why it is necessary to know the difference between list and tuple in python? Lists are used to store dynamic values where Tuples can store static values. In real life, we need to store the data in two ways. In the first way, we store the data in a data structure and later access the data and perform operations on the data. For example, the name of the students. We can store the names in a list and can access the names, insert new names, delete particular names.  In a second way, we store the data in a data structure and can access the data only without modifying the data. For example, the name of topper students for a particular year. We can store the topper names in a tuple because once declare, the names will not alter, we can only access the names. So, this is the basic difference between list and tuple in python. In this article, we will take a deeper look at the difference between Lists and Tuples in Python with an example. 

Tuple vs List in Python

          List and tuples in python are data structures. Both terms in Python are known as a collection of elements where each element is assigned a unique number known as an index. Entries in the Python List/Tuples are known as elements or items.  The main difference between List and Tuple in Python is, Python Lists are ordered and mutable. Whereas Python Tuples are ordered and immutable. Once we declare a tuple it cannot be modified or updated. When we talk about Tuple vs List in Python both are the data structures used to store the multiple values under a common name. Lists in Python are dynamic in nature but Tuples are static. Modification of the data inside the list is possible but we cannot modify the data inside the tuple because tuples are immutable. Tuples can be used in a situation where you don’t want to alter the data. This is all about Tuple vs List in Python. Let’s take a deeper look into the difference between List and Tuple in Python. 

Difference between List and Tuple in Python with examples

Parameters
Lists in Python
Tuples in Python
Nature
Mutable or changeable
Immutable or cannot change
Iteration
Iterating through Lists is time-consuming
Iterating through Tuples is not time-consuming
use 
Good for insertion-deletion
Good for accessing elements
memory
Requires more memory as compare to Tuples
Requires less memory as compared to Lists
deletion
The List can delete any particular element
Tuples cannot delete elements rather it can delete a whole tuple
creation
To create a List we can use the following waysdemo_List = [ ] #empty Listdemo_List = [1,2,3,4] #List with integer valuesdemo_List = ['Ram', 'Sham', 'Siya'] #List of strings
To create a Tuple we can use the following ways#Python Tuple exampledemo= ( ) #empty Tupledemo=(1, ) #Tuple with a single elementdemo= (1,2,3,4) #Tuple with integer valuesdemo= ('Ram', 'Sham', 'Siya') #Tuple of strings
accessing elements 
To get an entry from the List we use index numbers of the List. See the following example for more details:demo_List = ['Ram', 'Sham', 'Siya'] #List of stringsprint (demo_List [0]) # access the first elementprint (demo_List [1]) # access the second elementprint (demo_List [2]) # access the third elementOutput:RamShamSiya
To get an entry from the Tuple we use index numbers of the Tuple. See the following example for more details:demo_List = ['Ram', 'Sham', 'Siya'] #Tuple of stringsprint (demo_Tuple [0]) # access the first elementprint (demo_Tuple [1]) # access the second elementprint (demo_Tuple [2]) # access the third elementOutput:RamShamSiya  

Summary

          In this article, we discussed Python Tuples vs Lists, the difference between List and Tuple in Python with examples. Python lists are dynamic in nature whereas tuples are static in nature. We can access and modify the elements stored inside the list but we can only access the elements stored in a tuple and cannot modify the elements. We hope you enjoyed the article. If you have any related queries regarding the difference between List and Tuple in Python, feel free to ask in the comment section below. 
   
Liked what you read? Then don’t break the spree. Visit our blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube Channel to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing.

Submit Review