For working professionals
For fresh graduates
More
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
Foreign Nationals
The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not .
Recommended Programs
13. Print In Python
15. Python for Loop
19. Break in Python
23. Float in Python
25. List in Python
27. Tuples in Python
29. Set in Python
53. Python Modules
57. Python Packages
59. Class in Python
61. Object in Python
73. JSON Python
79. Python Threading
84. Map in Python
85. Filter in Python
86. Eval in Python
96. Sort in Python
101. Datetime Python
103. 2D Array in Python
104. Abs in Python
105. Advantages of Python
107. Append in Python
110. Assert in Python
113. Bool in Python
115. chr in Python
118. Count in python
119. Counter in Python
121. Datetime in Python
122. Extend in Python
123. F-string in Python
125. Format in Python
131. Index in Python
132. Interface in Python
134. Isalpha in Python
136. Iterator in Python
137. Join in Python
140. Literals in Python
141. Matplotlib
144. Modulus in Python
147. OpenCV Python
149. ord in Python
150. Palindrome in Python
151. Pass in Python
156. Python Arrays
158. Python Frameworks
160. Python IDE
164. Python PIP
165. Python Seaborn
166. Python Slicing
168. Queue in Python
169. Replace in Python
173. Stack in Python
174. scikit-learn
175. Selenium with Python
176. Self in Python
177. Sleep in Python
179. Split in Python
184. Strip in Python
185. Subprocess in Python
186. Substring in Python
195. What is Pygame
197. XOR in Python
198. Yield in Python
199. Zip in Python
Confused between a list, tuple, set, and dictionary in Python? They all seem similar at first—but use them wrong, and your code might break or slow down.
Think of it like organizing your stuff. A list is like a shopping list you can change; a tuple is a fixed list of rules you can't change, a set is a bag of unique items, and a dictionary is a phonebook for looking things up.
These four containers, the List, Tuple, Set, Dictionary in Python, are the backbone of how you store and manage data. This tutorial will a deep-dive into the difference between list tuple set and dictionary in python, showing you clear examples of when to use each one to write clean, efficient, and bug-free code.
We’ll walk you through list vs tuple vs set vs dictionary in Python, with hands-on examples. If you're looking to build deeper Python skills, explore our Data Science Courses and Machine Learning Courses—they cover real-world coding use cases using all these data structures.
A list in Python is an ordered collection of items, which means the elements in a list are stored in a specific order. You can store different data types in a list, such as strings, integers, or even other lists.
Lists are very versatile and are used frequently in Python programming.
Also Read: Spot Silent Bugs: Mutable and Immutable in Python You Must Know
Looking to bridge the gap between Python practice and actual ML applications? A formal Data Science and Machine Learning course can help you apply these skills to real datasets and industry workflows.
To create a list in Python, you simply use square brackets [] and separate elements with commas:
my_list = [element1, element2, element3, element4, element5]
There are many operations you can perform on lists. Here are a few common ones:
Ready to take your coding skills to the next level? Explore upGrad’s Online Software Development Courses and start building the software solutions of tomorrow. Let's get coding today!
Also Read: Difference Between List and Tuple in Python
A tuple in Python is similar to a list, but it’s immutable. The difference between list and tuple in python is that once you create a tuple, you cannot change its values. Tuples are ordered collections, meaning the items maintain their order.
They can hold multiple data types and are commonly used when you need a collection that should not be modified.
To create a tuple, you use parentheses () with items separated by commas:
my_tuple = (element1, element2, element3)
Here are common operations you can perform on tuples:
Also Read: What is Tuple in DBMS? Types, Examples & How to Work
A set in Python is an unordered collection of unique elements. Unlike lists or tuples, sets do not allow duplicate values, and they don’t maintain the order of elements. Sets are commonly used when you need to store unique items and perform set operations like unions, intersections, and differences.
To create a set, you use curly braces {} or the set() function:
my_set = {element1, element2, element3}
You can also create an empty set using set():
empty_set = set()
“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!”
A dictionary in Python is an unordered collection of data stored in key-value pairs. In simpler terms, it's like a real-world dictionary where each word (key) has a definition (value).
my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}
A comma separates each key-value pair.
Some important operations include:
Want to level up your coding skills? Dive into upGrad’s Data Structures & Algorithms Course, master the essentials, and solve complex problems with ease. Start your journey now!
Now that you have a basic understanding let’s compare them.
Parameter | List | Tuple | Set | Dictionary |
Ordered | Yes | Yes | No | Yes |
Mutable | Yes | No | Yes | Yes |
Duplicates Allowed | Yes | No | No | Yes |
Indexing | Yes | Yes | No | Yes |
Data Types | Can store any data type | Can store any data type | Can store any immutable data type | Stores key-value pairs (any data type) |
Performance | Slower for large data sets | Faster for large data sets | Fast membership tests | Fast for key-based access |
Syntax | [ ] | ( ) | { } | {key: value} |
Use Cases | Ordered collections, collections to modify | Fixed data, protection from changes | Unique items, set operations | Mapping unique keys to values |
Supports Operations | Add, remove, sort, slice | Slice, count, index | Add, remove, union, intersection | Add, remove, update, access by key |
Examples | [1, 2, 3] | (1, 2, 3) | {1, 2, 3} | {"a": 1, "b": 2} |
Let’s look at similarities next.
Also Read: 16+ Essential Python String Methods You Should Know (With Examples)
Let’s look at list, tuple, set, dictionary in Python with examples.
In this example, you’ll work with a dataset of employees in a company.
Let’s first create a dictionary that contains employee names and their corresponding employee IDs.
employees = [
{"Name": "Rahul", "Department": "IT"},
{"Name": "Priya", "Department": "HR"},
{"Name": "Amit", "Department": "Finance"},
{"Name": "Neha", "Department": "IT"}
]
Now, let’s extract just the employee names into a list:
employee_names = [employee["Name"] for employee in employees]
This line creates a new list called ‘employee_names’ that contains only the names of the employees by looping through the employees list and accessing the Name field.
Let’s print the list of employee names:
print(employee_names)
Output:
['Rahul', 'Priya', 'Amit', 'Neha']
Now, let’s perform some common list operations on our employee_names list.
# access the name of the second employee in the list
second_employee = employee_names[1]
print(second_employee)
Output:
Priya
The list is zero-indexed, so employee_names[1] refers to the second element in the list, which is "Priya".
# add a new employee to the list
employee_names.append("Suresh")
print(employee_names)
Output:
['Rahul', 'Priya', 'Amit', 'Neha', 'Suresh']
We use append() to add "Suresh" to the end of the employee_names list.
# remove "Amit" from the list
employee_names.remove("Amit")
print(employee_names)
Output:
['Rahul', 'Priya', 'Neha', 'Suresh']
The remove() method removes the first occurrence of the specified element, in this case, "Amit".
# check if "Neha" is in the list
is_neha_present = "Neha" in employee_names
print(is_neha_present)
Output:
True
The “in” keyword checks if the element "Neha" is present in the list. The result is True because "Neha" is indeed in the list.
Let’s first create a tuple to store student names along with their grades:
students_grades = (
("Rahul", 85),
("Priya", 90),
("Amit", 78),
("Neha", 92)
)
Suppose you want to access Amit’s grade:
amit_grade = students_grades[2][1]
print(amit_grade)
Output:
78
The first index students_grades[2] gets the tuple ("Amit", 78), and then [1] accesses Amit’s grade.
count_90 = sum(1 for student in students_grades if student[1] == 90)
print(count_90)
Output:
1
The sum() function counts how many times 90 appears in the grades of the students.
first_two_grades = students_grades[:2]
print(first_two_grades)
Output:
[('Rahul', 85), ('Priya', 90)]
This slices the first two student-grade tuples from the original students_grades tuple.
# returns the index of the first occurrence of a specified value
index_of_priya = students_grades.index(("Priya", 90))
print(index_of_priya)
Output:
1
# counts how many times a value appears in the tuple
grade_count = students_grades.count(("Neha", 92))
print(grade_count)
Output:
1
# combines two tuples into one
additional_students = (("John", 88), ("Maya", 95))
all_students = students_grades + additional_students
print(all_students)
Output:
[('Rahul', 85), ('Priya', 90), ('Amit', 78), ('Neha', 92), ('John', 88), ('Maya', 95)]
# repeats a tuple a specified number of times
repeated_grades = students_grades * 2
print(repeated_grades)
Output:
[('Rahul', 85), ('Priya', 90), ('Amit', 78), ('Neha', 92), ('Rahul', 85), ('Priya', 90), ('Amit', 78), ('Neha', 92)]
# extracts a subset of the tuple
subset = students_grades[1:3]
print(subset)
Output:
[('Priya', 90), ('Amit', 78)]
Let’s first create a set with unique product IDs:
store_products = {101, 102, 103, 104, 105}
Here, we’ve used curly braces {} to define a set of product IDs. Since sets don’t allow duplicates, if you try adding a duplicate value, it will be ignored.
Let’s say a new product with ID 106 arrives in the store:
store_products.add(106)
print(store_products)
Output:
{101, 102, 103, 104, 105, 106}
The add() method adds the product ID 106 to the set.
Now, let’s say product with ID 102 is discontinued and needs to be removed:
store_products.remove(102)
print(store_products)
Output:
{101, 103, 104, 105, 106}
The remove() method removes the specified element, in this case, 102. If the item doesn’t exist, it will raise an error.
Let’s check if product 104 is available:
is_product_available = 104 in store_products
print(is_product_available)
Output:
True
The in operator checks whether 104 is in the set and returns True if it exists.
Here are some important set operations that you can perform in Python:
# combines two sets and returns a new set with all unique elements from both sets
other_products = {107, 108, 109}
all_products = store_products | other_products
print(all_products)
Output:
{101, 103, 104, 105, 106, 107, 108, 109}
# returns a set of elements that are common to both sets
discontinued_products = {102, 103, 106}
common_products = store_products & discontinued_products
print(common_products)
Output:
{103, 106}
# returns a set of elements that are in one set but not in the other
remaining_products = store_products - discontinued_products
print(remaining_products)
Output:
{101, 104, 105}
# returns a set of elements that are in one set or the other, but not in both
unique_products = store_products ^ discontinued_products
print(unique_products)
Output:
{101, 104, 105, 102}
# checks if one set is a subset of another (i.e. if all elements in the first set are also in the second)
smaller_set = {103, 106}
is_subset = smaller_set <= store_products
print(is_subset)
Output:
True
# removes all elements from the set
store_products.clear()
print(store_products)
Output:
set()
Also Read: Difference Between Function and Method in Python
Let’s create a dictionary to store book titles and authors:
library_books = {
"The Alchemist": "Paulo Coelho",
"1984": "George Orwell",
"To Kill a Mockingbird": "Harper Lee",
"Pride and Prejudice": "Jane Austen"
}
Let’s say you want to add a new book to the library:
library_books["The Catcher in the Rye"] = "J.D. Salinger"
print(library_books)
Output:
{'The Alchemist': 'Paulo Coelho', '1984': 'George Orwell', 'To Kill a Mockingbird': 'Harper Lee', 'Pride and Prejudice': 'Jane Austen', 'The Catcher in the Rye': 'J.D. Salinger'}
Let’s remove "1984" from the library:
del library_books["1984"]
print(library_books)
Output:
{'The Alchemist': 'Paulo Coelho', 'To Kill a Mockingbird': 'Harper Lee', 'Pride and Prejudice': 'Jane Austen', 'The Catcher in the Rye': 'J.D. Salinger'}
If you want to update the author of "The Alchemist":
library_books["The Alchemist"] = "Paulo Coelho (Revised)"
print(library_books)
Output:
{'The Alchemist': 'Paulo Coelho (Revised)', 'To Kill a Mockingbird': 'Harper Lee', 'Pride and Prejudice': 'Jane Austen', 'The Catcher in the Rye': 'J.D. Salinger'}
Let’s check if "Pride and Prejudice" exists in the library:
is_book_present = "Pride and Prejudice" in library_books
print(is_book_present)
Output:
True
The “in” operator checks if "Pride and Prejudice" is a key in the dictionary and returns True since it exists.
Here are some important dictionary operations you can perform in Python:
# retrieves the value associated with a given key
author = library_books.get("1984", "Not Found")
print(author)
Output:
Not Found
# returns a view object of all keys in the dictionary
keys = library_books.keys()
print(keys)
Output:
dict_keys(['The Alchemist', 'To Kill a Mockingbird', 'Pride and Prejudice', 'The Catcher in the Rye'])
# returns a view object of all values in the dictionary
values = library_books.values()
print(values)
Output:
dict_values(['Paulo Coelho (Revised)', 'Harper Lee', 'Jane Austen', 'J.D. Salinger'])
# returns a view object of all key-value pairs in the dictionary
items = library_books.items()
print(items)
Output:
dict_items([('The Alchemist', 'Paulo Coelho (Revised)'), ('To Kill a Mockingbird', 'Harper Lee'), ('Pride and Prejudice', 'Jane Austen'), ('The Catcher in the Rye', 'J.D. Salinger')])
# removes a key-value pair and returns the value
removed_book = library_books.pop("Pride and Prejudice")
print(removed_book)
print(library_books)
Output:
Jane Austen{'The Alchemist': 'Paulo Coelho (Revised)', 'To Kill a Mockingbird': 'Harper Lee', 'The Catcher in the Rye': 'J.D. Salinger'}
# removes all items from the dictionary
library_books.clear()
print(library_books)
Output:
{}
These were the list, tuple, set, dictionary in Python with example, showcasing how each data structure can be used in different scenarios.
To learn more about data structures, OOPs, etc., join upGrad’s Programming with Python: Introduction for Beginners course and kickstart your programming journey!
Let’s take a look at some common use cases where lists shine and how they differ from other data structures.
Also Read:What Are Attributes in DBMS ? 10 Types and Their Practical Role in Database Design
Tuples are widely used when you need a fixed collection of items. Here are some practical applications of tuples:
Also Read: How to Take Multiple Input in Python: Techniques and Best Practices
Sets in Python are used when you need to store unique items. Here are some common use cases:
Dictionaries in Python are handy for mapping unique keys to values. Here are some common use cases:
Also Read: 12 Incredible Applications of Python You Should Know About
Understanding the difference between list, tuple, set, dictionary in Python with example will help you make the right choice for your project.
1. Which of the following data types is mutable in Python?
a) List
b) Tuple
c) Set
d) Both a and c
2. What is the correct syntax to create a dictionary?
a) d = (1: 'a', 2: 'b')
b) d = [1: 'a', 2: 'b']
c) d = {1: 'a', 2: 'b'}
d) d = <1: 'a', 2: 'b'>
3. Which of the following stores elements in key-value pairs?
a) List
b) Tuple
c) Set
d) Dictionary
4. Which of these data structures does not allow duplicate values?
a) List
b) Set
c) Dictionary
d) Tuple
5. What is the main difference between a tuple and a list?
a) Tuple is ordered, list is unordered
b) List is immutable, tuple is mutable
c) Tuple is immutable, list is mutable
d) List uses (), tuple uses []
6. Which of the following allows indexing and is unordered?
a) List
b) Tuple
c) Set
d) Dictionary
7. What will len({1, 2, 2, 3}) return?
a) 3
b) 4
c) 2
d) Error
8. Which one is the correct syntax to access a value in a dictionary by its key?
a) dict.key
b) dict->key
c) dict[key]
d) dict{key}
9. You are storing user names where order and duplication matter. Which Python data type will you choose?
a) Set
b) Dictionary
c) List
d) Tuple
10. A developer wants to map employee IDs to names. Which is the best data type?
a) Tuple
b) List
c) Set
d) Dictionary
11. You receive a list of country names. You need to remove duplicates and ignore order. Which will you use?
a) Dictionary
b) List
c) Tuple
d) Set
In conclusion, mastering the difference between list tuple set and dictionary in python is a fundamental step in becoming a proficient Python developer. These four data structures are the essential building blocks for organizing information and knowing when to use each one is the key to writing efficient, readable, and bug-free code.
This guide has equipped you with the core knowledge of the List, Tuple, Set, Dictionary in Python. Remember: use lists for ordered, mutable collections; tuples for fixed, immutable data; sets for unique items; and dictionaries for fast, key-value lookups. Keep practicing, and you'll soon be choosing the perfect data structure for any problem with confidence.
With upGrad, you can access global standard education facilities right here in India. upGrad also offers free Data Science 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 Data Science 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 main difference between list tuple set and dictionary in python in the case of a list and tuple is mutability. A list, defined with square brackets [], is mutable, meaning you can add, remove, or change its elements after it has been created. A tuple, defined with parentheses (), is immutable, so once it's created, its contents are locked in and cannot be altered. This makes lists ideal for collections of data that need to change, and tuples perfect for fixed data that should remain constant.
There are two key differences. First, lists are ordered collections, meaning they maintain the insertion order of elements, and you can access elements by their index. Sets, on the other hand, are unordered, so you cannot rely on the elements being in any specific sequence. Second, lists can contain duplicate values, while sets enforce uniqueness—they automatically discard any duplicate items you try to add. This is a crucial part of the difference between list, tuple and set.
You should use a dictionary when you need to store data as a collection of key-value pairs, which allows for fast and efficient lookups based on a unique key. This is ideal for scenarios like storing user information where you can look up a user's details by their unique username. You should use a list when you simply need an ordered collection of items that you can access by their numerical index, like a list of names or a sequence of steps.
No, tuples are strictly immutable, which is one of their defining characteristics. Once a tuple is created, you cannot change, add, or remove any of its elements. If you need to "modify" a tuple, you would have to create a new tuple that contains the desired changes. This immutability provides a form of data integrity, ensuring that the collection of items remains constant throughout your program.
If you attempt to add an item to a set that already exists in the set, the set will simply do nothing. Because sets are designed to store only unique values, the duplicate item is automatically ignored, and the set remains unchanged. This is a core feature and is what makes sets so useful for tasks like removing duplicate elements from a list.
No, all keys in a Python dictionary must be unique. If you try to add a new key-value pair with a key that already exists in the dictionary, the new value will simply overwrite the old value associated with that key. This is an intentional design, as the key's purpose is to serve as a unique identifier for its corresponding value.
Yes, absolutely. The values in a dictionary can be any Python data type, including other complex data structures. It is very common to have a dictionary where a value is a list, another dictionary, or a tuple. For example, you could have a dictionary that maps a student's name to a list of their grades: {'Alice': [88, 92, 95]}. This makes the List, Tuple, Set, Dictionary in Python extremely flexible.
Yes, you can use a tuple as a dictionary key. This is possible because dictionary keys must be of an immutable type, and tuples are immutable. Lists, being mutable, cannot be used as dictionary keys. Using a tuple as a key can be useful in situations where you need a compound key, such as using (latitude, longitude) as a key to store data about a specific location.
You can create an empty version of each of these data structures with the following syntax:
You can access elements in lists and tuples using their numerical index in square brackets, starting from 0 (e.g., my_list[0]). In a dictionary, you access a value using its corresponding key in square brackets (e.g., my_dict['key']). Sets are unordered and do not support indexing, so you cannot access elements by position. Instead, you can check for an element's presence using the in keyword.
Yes, a set can store a mix of different immutable data types, such as integers, strings, and tuples. However, a set cannot contain mutable data types like lists or dictionaries. This is because the elements of a set must be "hashable," a property that mutable objects do not have. This is a key part of the difference between list, tuple and set.
The performance differences are significant. For checking if an item is present in a collection (membership testing), sets are much faster (average O(1) time complexity) than lists and tuples (O(n) time complexity). Tuples are generally more memory-efficient and slightly faster to create and access than lists because they are immutable.
No, lists do not have built-in methods for set-based mathematical operations. These operations—such as union, intersection, and difference—are a core feature of sets. To perform these operations on the elements of a list, you would first need to convert the list (or lists) into sets, perform the operation, and then, if needed, convert the resulting set back into a list.
Python makes it very easy to convert between these types using their built-in constructor functions. For example, you can convert a list to a tuple with tuple(my_list), a list to a set (which will remove duplicates) with set(my_list), or a tuple back to a list with list(my_tuple). This is a common pattern when you need to leverage the features of a different data structure, like using a set to de-duplicate a list.
A frozenset is an immutable version of a regular set. Once a frozenset is created, you cannot add or remove elements from it. Because it is immutable and therefore hashable, you can use a frozenset as an element in another set or as a key in a dictionary, which you cannot do with a regular, mutable set.
Lists are used for any ordered sequence of items, like a list of to-do items or user posts on a social media feed. Tuples are used for fixed collections of data, like returning multiple values from a function or storing database records. Sets are used for membership testing and removing duplicates, like finding the unique visitors to a website. Dictionaries are the foundation of many programming tasks, used for everything from parsing JSON data to storing the attributes of an object.
To add an element to a list, you use the .append() method to add to the end or .insert() to add at a specific index. To add an element to a set, you use the .add() method. To add a new key-value pair to a dictionary, you use simple key assignment: my_dict['new_key'] = 'new_value'. Remember, you cannot add elements to a tuple as it is immutable.
To remove an element from a list, you can use .remove() (by value) or .pop() (by index). For a set, you can use .remove() (which will raise an error if the element is not found) or .discard() (which will not raise an error). To remove a key-value pair from a dictionary, you use the del keyword: del my_dict['key'].
The best way to learn is through a combination of structured education and hands-on practice. A comprehensive program, like the Python programming courses offered by upGrad, can provide a strong foundation by teaching you the theory behind the List, Tuple, Set, Dictionary in Python. You should then apply this knowledge by building small projects and solving coding challenges, which will help you master their practical use cases.
The main takeaway is that the difference between list tuple set and dictionary in python is all about choosing the right tool for the job. You need to consider three key properties: mutability (can it be changed?), order (is the sequence important?), and uniqueness (are duplicates allowed?). By asking these three questions, you can confidently choose the most efficient and appropriate data structure for any programming task you face.
Take our Free Quiz on Python
Answer quick questions and assess your Python knowledge
FREE COURSES
Start Learning For Free
Author|900 articles published