For working professionals
For fresh graduates
More
In Python, List and Tuples are built-in data structures for storing and managing the collections of data. A list is like a flexible notebook where you can add, remove, or rearrange items. On the other hand, a tuple is like a sealed envelope — once packed, its contents remain unchanged. While they might look similar, the differences between list and tuple are really important for how you’ll use them in your programs.
It can be a bit tricky to know when to use a list or a tuple. Many developers find themselves mixing them up, which can lead to slower code, unexpected bugs, or even wasted resources. This confusion usually stems from not fully knowing their unique features and the best scenarios to use them in.
But don’t worry! It’s actually pretty easy to sort this out. By learning about key distinctions like mutability, performance, and use cases, you'll be able to confidently decide when to use each one.
In this article, we will explore what is list, what is tuple and the key difference between tuple and list in Python with the help of examples. Whether you’re just starting with Python or looking to sharpen your skills, this guide will help you write smarter and more efficient code. Happy coding!
“Enhance your Python skills further with our Data Science and Machine Learning courses from top universities — take the next step in your learning journey!”
A list in Python is a built-in data structure that enables you to store a collection of items. These items can vary in data types, including integers, strings, or even other lists.
Lists are among the most versatile and frequently used data structures in Python because they are mutable, which means you can modify their contents after they've been created.
list_name = [element1, element2, element3, ...]
Also Read: Lists Methods in Python
A tuple in Python is an ordered and immutable collection of elements. It is similar to a list, but unlike lists, tuples cannot be modified after they are created.
Tuples are defined using parentheses () and can hold elements of various data types, including integers, strings, floats, and even other collections.
tuple_name = (element1, element2, element3, ...)
“Start your coding journey with our complimentary Python courses designed just for you — dive into Python programming fundamentals, explore key Python libraries, and engage with practical case studies!”
Parameter | List | Tuple |
Definition | A list is an ordered, mutable collection of items. | A tuple is an ordered, immutable collection of items. |
Syntax | Defined using square brackets [ ]. | Defined using parentheses ( ) . |
Mutability | Mutable: Elements can be added, removed, or modified. | Immutable: Elements cannot be changed once the tuple is created. |
Performance | Slower due to dynamic resizing and mutability. | Faster due to immutability and smaller memory footprint. |
Memory Usage | Consumes more memory as it supports additional functionalities like append or pop. | Consumes less memory as it has fewer functionalities. |
Methods Available | Provides methods like append(), remove(), sort(), etc. | Limited methods like count() and index(). |
Use Case | Suitable for dynamic collections where data changes frequently. | Ideal for fixed data that does not require modification. |
Iteration Speed | Slower than tuples during iteration. | Faster due to immutability and reduced overhead. |
Error Safety | More prone to accidental changes or bugs. | Safer as the data remains constant throughout the program. |
Nesting Capability | Can nest lists, tuples, or other collections. | Can also nest tuples or other collections. |
Type Conversion | A list can be converted to a tuple using tuple(). | A tuple can be converted to a list using list(). |
Let’s create an employee dataset, and then perform different operations of list on that.
# Dataset of employee details with Indian names
employee_data = [
{"Name": "Aditi", "Department": "HR"},
{"Name": "Rahul", "Department": "IT"},
{"Name": "Sneha", "Department": "Finance"}
]
# Extracting employee names into a list
employee_names = [employee["Name"] for employee in employee_data]
# Printing the list of names
print("List of Employee Names:", employee_names)
Output
List of Employee Names: ['Aditi', 'Rahul', 'Sneha']
Now, let’s perform different Python list operations
# List of employee names (extracted from the dataset)
employee_names = ['Aditi', 'Rahul', 'Sneha']
# Accessing elements in the list
print("First Employee:", employee_names[0]) # Access the first element
print("Last Employee:", employee_names[-1]) # Access the last element
# Adding a new employee to the list
employee_names.append('Ankit')
print("Updated List of Employees:", employee_names)
# Removing an employee from the list
employee_names.remove('Rahul')
print("After Removing Rahul:", employee_names)
# Iterating through the list
print("Employee Names:")
for name in employee_names:
print(name)
# Checking if an employee exists in the list
if 'Sneha' in employee_names:
print("Sneha is in the list.")
else:
print("Sneha is not in the list.")
Output
First Employee: Aditi
Last Employee: Sneha
Updated List of Employees: ['Aditi', 'Rahul', 'Sneha', 'Ankit']
After Removing Rahul: ['Aditi', 'Sneha', 'Ankit']
Employee Names:
Aditi
Sneha
Ankit
Sneha is in the list.
Here, we will do same. First we will create a dataset and then perform operations on tuple.
# Tuple of city weather data
weather_data = (
("Mumbai", 32, 75),
("Delhi", 38, 50),
("Chennai", 34, 80),
("Kolkata", 30, 85),
)
# Access weather data for Mumbai
mumbai_weather = weather_data[0]
print("Weather data for Mumbai:", mumbai_weather)
# Display weather data for all cities
for city, temperature, humidity in weather_data:
print(f"City: {city}, Temperature: {temperature}°C, Humidity: {humidity}%")
# Find the city with the highest temperature
hottest_city = max(weather_data, key=lambda x: x[1])
print("Hottest city:", hottest_city[0])
# Convert the tuple to a list to add new city data
weather_data_list = list(weather_data)
weather_data_list.append(("Bengaluru", 28, 70))
weather_data = tuple(weather_data_list) # Convert back to a tuple
print("Updated weather data:", weather_data)
Output
Weather data for Mumbai: ('Mumbai', 32, 75)
City: Mumbai, Temperature: 32°C, Humidity: 75%
City: Delhi, Temperature: 38°C, Humidity: 50%
City: Chennai, Temperature: 34°C, Humidity: 80%
City: Kolkata, Temperature: 30°C, Humidity: 85%
Hottest city: Delhi
Updated weather data: (('Mumbai', 32, 75), ('Delhi', 38, 50), ('Chennai', 34, 80), ('Kolkata', 30, 85), ('Bengaluru', 28, 70))
With upGrad, you can access global standard education facilities right here in India. upGrad also offers free Python courses that come with certificates, making them an excellent opportunity if you're interested in data science and machine learning.
By enrolling in upGrad's Python courses, you can benefit from the knowledge and expertise of some of the best educators from around the world. These instructors understand the diverse challenges that Python programmers face and can provide guidance to help you navigate them effectively.
So, reach out to an upGrad counselor today to learn more about how you can benefit from a Python course.
Here are some of the best data science and machine learning courses offered by upGrad, designed to meet your learning needs:
Similar Reads: Top Trending Blogs of Python
The choice between a list and a tuple depends on your program's specific needs:
Tuples are designed to be immutable to provide data integrity and allow for optimizations:
Yes, tuples can contain duplicate elements.
Yes, you can concatenate two tuples using the + operator.
You can convert a list to a tuple using the tuple() constructor.
Generally, tuples can be slightly faster than lists due to their immutability, which allows for optimizations. However, the performance difference is usually minimal and should not be the sole factor in choosing between them.
Tuples can be created by placing comma-separated values within parentheses
Ready to challenge yourself? Take our Free Python Quiz!
Pavan Vadapalli
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.