Python is one of the most popular programming languages because of its simple syntax and powerful data structures. Among them, list, tuple, and dictionary are three commonly used collections that allow developers to store and manipulate data.
Although they may look similar at first, they serve different purposes. Let’s dive deeper into their differences.
📋 What is a List?
A list is an ordered, mutable collection in Python.
It allows duplicate elements and can store mixed data types (integers, strings, floats, etc.).
Lists are created using square brackets
[].
✅ Example
my_list = [10, "Hello", 3.14, True]
print(my_list[1]) # Output: Hello
my_list.append("Python") # Adding new elementprint(my_list) # [10, 'Hello', 3.14, True, 'Python']🔒 What is a Tuple?
A tuple is an ordered, immutable collection.
Once created, you cannot change (add, remove, or modify) its elements.
Tuples are created using parentheses
().Since they are immutable, they are faster than lists.
✅ Example
my_tuple = (5, "World", 2.71, False)
print(my_tuple[0]) # Output: 5
# my_tuple[1] = "New" ❌ This will throw an error (immutable)📖 What is a Dictionary?
A dictionary is an unordered, mutable collection.
It stores data in key-value pairs.
Keys must be unique and immutable (like strings, numbers, tuples), while values can be of any type.
Dictionaries are created using curly braces
{}.
✅ Example
my_dict = {"name": "Alice", "age": 25, "is_student": True}
print(my_dict["name"]) # Output: Alice
my_dict["city"] = "New York" # Adding a new key-value pairprint(my_dict)
# {'name': 'Alice', 'age': 25, 'is_student': True, 'city': 'New York'}
Join the conversation! Your thoughts help the community grow.