Difference Between List and Tuple in Python
Confused with what list and tuples in Python. Don’t worry the article will clear all your doubts.
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
Best-suited Python courses for you
Learn Python with these high-rated online courses
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).
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
Output
Accessing Elements in List
# access the elements
print (fruit[0])print (fruit [2])
Output
Modifying Elements in List
#modifying elements
fruit [2] = 'pomegranate'fruit
Output
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
Output
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
Output
Slicing the Elements
# slicing the element# if input [start, end], then it will pick out the element [start, end - 1]
print(fruit[1:3])
Output
Concatenating Two Lists
# concatenation
veggie = ['carrot', 'potato', 'tomato' ]
new_list = fruit + veggieprint(new_list)
Output
Must Check: Top Python Online Courses and Certifications
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.
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
Output
Accessing Elements in Tuple
#accessing elements
print(fruits[0]) print(fruits[2])
Output
Tuple is Mutable or Not
#Checking mutability
fruits[1] = 'grape'
Output
Concatenation
#concatenation
vegetables = ('carrot', 'spinach')veggie = fruits + vegetablesprint(veggie)
Output
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!!
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