Difference Between List and Tuple in Python

Difference Between List and Tuple in Python

3 mins read1.4K Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 3, 2023 11:35 IST

Confused with what list and tuples in Python. Don’t worry the article will clear all your doubts.

2022_06_f12.jpg

List and Tuple in Python are the data structures that store and manipulate a collection of elements. Both are used to store the data, and the values stored can be accessed using indexes. But the key difference between both is that lists are mutable, and tuples are immutable.
In this article, we will discuss what lists and tuples in Python are and the key differences between lists and tuples with the help of examples.

Must Read: What is Python?

Must Check: Free Online Python Courses and Certifications

So, let’s start with the tabular difference between List and Tuple.

Table of Content

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

What is the Difference Between List and Tuple?

Parameter List Tuple
Mutability Mutable Immutable
Syntax Enclosed in Square ([]) bracket Enclosed in Square (()) bracket
Length It can be changed, i.e., a new element can be added. Length is fixed, i.e., new elements can’t be added.
Order Elements order is preserved. Elements order is preserved.
Usage It is used to store information that needs to be modified or changed over time.Examples: Employee Record It’s used to store information that doesn’t need to alter over time.Example: Coordinates of a point in a map.
Performance Requires additional memory allocation and deallocation when modified.  It is more efficient and faster to access than the list.
Operation Elements can be added, removed, or modified. It supports indexing, slicing, and iterating over elements.
Example Employee = [‘A’, ‘B’, ‘C’] Employee Number = (1001, 1002, 1003)

What is a List in Python?

A list is a collection of various data types, such as integers, floats, strings, objects, or lists. The elements in the list are separated by commas and are stored in the square bracket ([]). Lists are mutable (i.e., elements can be modified after the creation), and ordered (i.e., elements are indexed). The index in the list starts from the first element (index = 0) to the last element (index = number of elements – 1).

All About Python Lists Methods
Python Lists Practice Programs For Beginners
How to Convert a Python List to String?

Now, let’s have some examples of how to create a list, access, modify, add, remove the elements from the list and many more.

Create a List


 
fruit = ['apple', 'banana', 'mango', 'orange']
fruit
Copy code

Output

2023_05_create-a-list.jpg

Accessing Elements in List


 
# access the elements
print (fruit[0])
print (fruit [2])
Copy code

Output

2023_05_access-the-elements-1.jpg

Modifying Elements in List


 
#modifying elements
fruit [2] = 'pomegranate'
fruit
Copy code

Output

2023_05_modifying-list.jpg

Adding Elements in the List


 
# adding element in the list
# append: add element at the end of the list
fruit.append ('kiwi')
#insert: add the element at the defined index
fruit.insert( 1, 'cherry')
fruit
Copy code

Output

2023_05_adding-elements-in-the-list.jpg

Removing Elements in the List


 
# removing element
# remove: it will remove the given element from the list
fruit.remove ('kiwi')
#del: remove the element at the defined index
del fruit[1]
fruit
Copy code

Output

2023_05_removing-elements-from-list.jpg

Slicing the Elements


 
# slicing the element
# if input [start, end], then it will pick out the element [start, end - 1]
print(fruit[1:3])
Copy code

Output

2023_05_slicing-the-elements-in-list-1.jpg

Concatenating Two Lists


 
# concatenation
veggie = ['carrot', 'potato', 'tomato' ]
new_list = fruit + veggie
print(new_list)
Copy code
Python’s pop() Method: Simplifying List Manipulation and Data Removal
Python Sort List
Difference Between List and Set

Explore the leading job-centric courses for life after 12th. Also, embark on specialized online degree programs for a fulfilling career journey.

What is a Tuple in Python?

Similar to the list, tuples are also the data structure in Python, with the key difference that they are immutable (i.e., elements can’t be modified after creation). In Python, Tuples are widely used where data integrity, order preservation, and immutability are important. The order of the elements in tuples is preserved, which allows for easy access and manipulation using indexes. It can store elements of any data type, including numbers, strings, Boolean, or even another tuple.

Understanding Tuples in Python
Efficient Python Testing with PyTest: Tips and Tricks
Types of Functions in Python

Now, let’s have another example of how to create a tuple, access, modify, add, remove the elements from the list and many more.

Creating a Tuple


 
# create a tuple
fruits = ('apple', 'banana', 'orange', 'mango')
fruits
Copy code

Output

2023_05_creating-a-tuple-in-python.jpg

Accessing Elements in Tuple


 
#accessing elements
print(fruits[0])
print(fruits[2])
Copy code

Output

2023_05_accessing-tuple-elements-2.jpg

Tuple is Mutable or Not


 
#Checking mutability
fruits[1] = 'grape'
Copy code

Output

2023_05_tuples-are-immutable.jpg

Concatenation


 
#concatenation
vegetables = ('carrot', 'spinach')
veggie = fruits + vegetables
print(veggie)
Copy code

Output

2023_05_concatenating-tuples-1.jpg

Key Differences Between List and Tuple in Python

  • Lists are mutable, while tuples are immutable. 
    • i.e., the elements of the list can be changed, after the creation, while the elements of the tuples can’t be modified.
  • The size of the tuples is fixed, while the list can have a variable size.
    • i.e., elements can be added or removed from the list but can’t be done in the tuple.
  • Since tuples are immutable, they are generally faster than the list.
  • Lists are used for data that needs to be changed frequently, while tuples are used for data that doesn’t need to be changed frequently.

Conclusion

In this article, we have briefly discussed what list and tuples are in python and the difference between them with the help of different examples. Hope you will like the article.

Keep Learning!!

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio