When writing any real program, you need to store and organize data.
Whether it’s a to-do list, a price catalog, or a user profile Python gives you powerful built-in data types that let you store multiple items in one place.
In this detailed lesson, you’ll learn:
- What Lists, Tuples, and Dictionaries are
- How to create, access, and modify them
- When to use which data type
- Common mistakes and how to avoid them
- Real-life examples for each
Let’s get into it slowly and clearly.
🧺 What Are Collections in Python?
Collections let you store multiple values in one variable.
Python has several, but the most common are:
List
– an ordered, changeable group of itemsTuple
– an ordered, unchangeable group of itemsDictionary
– a collection of key-value pairs
📝 Lists in Python
🔹 What Is a List?
A list is a collection of ordered, changeable items.
Python
fruits = ["apple", "banana", "cherry"]
You can store:
- Strings
- Numbers
- Other lists
- Even mixed data types
🔍 Accessing List Items
Python
print(fruits[0]) # apple
print(fruits[-1]) # cherry (last item)
✏️ Modifying a List
Python
fruits[1] = "mango"
print(fruits) # ['apple', 'mango', 'cherry']
➕ Adding Items
Python
fruits.append("orange")
fruits.insert(1, "kiwi") # insert at position 1
❌ Removing Items
Python
fruits.remove("apple")
popped = fruits.pop() # removes last item
🧪 Other List Operations
Python
print(len(fruits))
print("mango" in fruits)
fruits.sort()
fruits.reverse()
🧠 Real-World List Use Case
Python
todo = []
task = input("Enter a task: ")
todo.append(task)
print("Your Tasks:")
for t in todo:
print("-", t)
📍 Tuples in Python
🔸 What Is a Tuple?
A tuple is like a list but immutable (unchangeable).
Python
colors = ("red", "green", "blue")
You can access items like a list:
Python
print(colors[1]) # green
But you cannot change them:
Python
colors[1] = "yellow" # ❌ Error!
✅ When to Use a Tuple?
- When your data shouldn’t change (e.g. coordinates, fixed settings)
- Slightly faster than lists
🧪 Tuple Example: Coordinates
Python
location = (24.8763, 67.0250) # latitude, longitude
📦 Dictionaries in Python
🔑 What Is a Dictionary?
A dictionary stores key-value pairs. It’s like a labeled container.
Python
person = {
"name": "Ali",
"age": 28,
"is_student": True
}
🔍 Accessing Dictionary Values
Python
print(person["name"]) # Ali
You can also use:
Python
print(person.get("age"))
✏️ Modifying a Dictionary
Python
person["age"] = 29
person["city"] = "Lahore"
❌ Removing Items
Python
del person["is_student"]
🔁 Looping Through a Dictionary
Python
for key, value in person.items():
print(f"{key}: {value}")
🧠 Real-World Dictionary Use Case
Python
product = {
"id": 101,
"name": "Laptop",
"price": 45000,
"in_stock": True
}
if product["in_stock"]:
print(f"{product['name']} is available for ₹{product['price']}")
🧱 List vs Tuple vs Dictionary – At a Glance
Feature | List | Tuple | Dictionary |
---|---|---|---|
Ordered | ✅ Yes | ✅ Yes | ❌ Not by position (by key) |
Mutable | ✅ Yes | ❌ No | ✅ Yes |
Indexed | ✅ Yes (0,1,2…) | ✅ Yes | ❌ Keys instead of indexes |
Use Case | General items | Fixed items | Structured data with labels |
⚠️ Common Mistakes
Mistake | Fix |
---|---|
Trying to change a tuple | Tuples can’t be modified. Use a list if you need flexibility. |
Accessing wrong key in dict | Use dict.get("key") to avoid crash if the key doesn’t exist. |
Using string index on int list | Remember: "0" ≠ 0 strings and ints are different types |