Overview
So far, you’ve learned to work with single values like numbers or strings. But in real-world tasks, you often deal with collections of items:
• A list of student names
• A dictionary of countries and their capitals
• A set of unique user IDs
This is where data structures come in. They help you group, store, and organize multiple values efficiently.
⸻
1. Lists – Ordered, Changeable Collections
Analogy:
A list is like a shopping basket. Each item has a specific order, and you can add, remove, or change things as needed.
Creating a List
fruits = [“apple”, “banana”, “cherry”]
Accessing Items by Index
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: cherry
Modifying an Item
fruits[1] = “orange”
print(fruits) # Output: [‘apple’, ‘orange’, ‘cherry’]
Adding Items
fruits.append(“grape”) # Adds to the end
fruits.insert(1, “kiwi”) # Inserts at index 1
print(fruits) # [‘apple’, ‘kiwi’, ‘orange’, ‘cherry’, ‘grape’]
Removing Items
fruits.remove(“orange”)
print(fruits) # [‘apple’, ‘kiwi’, ‘cherry’, ‘grape’]
fruits.pop() # Removes the last item
print(fruits)
Looping Through a List
for fruit in fruits:
print(“I like”, fruit)
⸻
2. Tuples – Ordered, Immutable Collections
Analogy:
A tuple is like a locked briefcase. You know what’s inside, and the order matters, but you can’t change the contents once it’s packed.
Creating a Tuple
dimensions = (1920, 1080)
Accessing Items
print(dimensions[0]) # Output: 1920
Why Use Tuples?
• Safer when you don’t want values to be changed accidentally
• Often used for fixed collections (like coordinates or screen sizes)
⸻
3. Dictionaries – Key-Value Pairs
Analogy:
A dictionary is like a real-world dictionary: you look up a word (key) to find its definition (value).
Creating a Dictionary
capitals = {
“France”: “Paris”,
“Japan”: “Tokyo”,
“India”: “New Delhi”
}
Accessing Values
print(capitals[“Japan”]) # Output: Tokyo
Adding or Changing Values
capitals[“Germany”] = “Berlin”
capitals[“India”] = “Delhi” # Update
Removing Entries
del capitals[“France”]
Looping Through a Dictionary
for country, city in capitals.items():
print(country, “→”, city)
⸻
4. Sets – Unordered, Unique Items
Analogy:
A set is like a collection of club members: each person is unique, and the order doesn’t matter.
Creating a Set
unique_numbers = {1, 2, 3, 3, 4}
print(unique_numbers) # Output: {1, 2, 3, 4}
Explanation: Sets automatically remove duplicates.
Adding and Removing Items
unique_numbers.add(5)
unique_numbers.discard(2)
Why Use Sets?
• To remove duplicates
• For fast membership testing (checking if an item exists)
⸻
5. Summary Table
Type Ordered Changeable Duplicate Allowed Syntax
List Yes Yes Yes []
Tuple Yes No Yes ()
Dict Keys: No Yes Keys: No {}
Set No Yes No {}
⸻
6. Real-Life Example: Student Grades System
Let’s combine data structures into a simple application:
# Dictionary of students and their grades
grades = {
“Alice”: [90, 85, 88],
“Bob”: [78, 81, 74],
“Charlie”: [92, 90, 95]
}
# Print average grade for each student
for student, marks in grades.items():
average = sum(marks) / len(marks)
print(student, “Average Grade:”, average)
Output:
Alice Average Grade: 87.666…
Bob Average Grade: 77.666…
Charlie Average Grade: 92.333…
⸻
Conclusion
In this unit, you learned how to use data structures to store and organize multiple values:
• Lists: Ordered, editable sequences
• Tuples: Ordered, unchangeable collections
• Dictionaries: Key-value lookups
• Sets: Unordered collections with unique items
These tools help you manage real-world data in Python and prepare you for more advanced programming tasks like databases, APIs, and algorithms.