The Collections
All the notes. All the examples. All in one place. The challenges prove you understand it. Solve them all and you mastered The Collections.
▶📝 Cheatsheet
1. Lists
A variable holds one thing. A list holds many things. Your phone contacts, your playlist, your grocery list. One name, many items inside.
fruits = ["apple", "banana", "mango"]print(fruits)Accessing items
print(fruits[0]) # appleprint(fruits[-1]) # mangoAdding and removing
fruits.append("orange")fruits.remove("banana")print(fruits) # ["apple", "mango", "orange"]Length
print(len(fruits)) # 3Looping through a list
for fruit in fruits: print(fruit)Check if item exists
if "mango" in fruits: print("Yes!")Sorting
numbers = [5, 2, 8, 1, 9]numbers.sort()print(numbers) # [1, 2, 5, 8, 9]2. Tuples & Sets
Tuples
List but can't be changed
grades = (88, 72, 95, 60, 84)print(grades[0]) # 88
grades[0] = 90 # ERROR - can't change a tupleWhen to use tuples
days = ("Mon", "Tue", "Wed", "Thu", "Fri")months = ("Jan", "Feb", "Mar", "Apr", "May")Sets
List with no duplicates
cities = {"Oslo", "London", "Oslo", "Nairobi", "London"}print(cities) # {"Oslo", "London", "Nairobi"}Removing duplicates from a list
names = ["Ahmed", "Hodan", "Ahmed", "Nimo", "Hodan"]unique = set(names)print(unique) # {"Ahmed", "Hodan", "Nimo"}3. Dictionaries
A list uses numbers to find things. A dictionary uses names. You look up the key, you get the value.
student = { "name": "Ahmed", "age": 22, "city": "Oslo"}print(student["name"]) # AhmedAdding and updating
student["email"] = "ahmed@email.com"student["age"] = 23List of dictionaries
students = [ {"name": "Ahmed", "score": 85}, {"name": "Hodan", "score": 92}, {"name": "Bile", "score": 67}]
for s in students: print(f"{s['name']}: {s['score']}")🧾 Reference Table
| Concept | Syntax | Example |
|---|---|---|
| Create a list | [item, item] | fruits = ["apple", "banana"] |
| Access item | list[index] | fruits[0] |
| Last item | list[-1] | fruits[-1] |
| Add item | .append() | fruits.append("mango") |
| Remove item | .remove() | fruits.remove("apple") |
| Length | len() | len(fruits) |
| Check if exists | in | "apple" in fruits |
| Sort | .sort() | numbers.sort() |
| Create a tuple | (item, item) | grades = (88, 72, 95) |
| Create a set | {item, item} | cities = {"Oslo", "London"} |
| Remove duplicates | set() | set(names) |
| Create a dict | {"key": value} | {"name": "Ahmed"} |
| Access value | dict["key"] | student["name"] |
| Add/update | dict["key"] = value | student["age"] = 23 |
| All values | .values() | student.values() |
▶🧩 Challenges
Challenge 1: My Friends
Create a list of 4 friends. Print the first one and the last one.
First: AhmedLast: NimoChallenge 2: Add and Remove
Create a list: ["Rice", "Chicken", "Bread"]. Add "Milk" to the end. Remove "Chicken". Print the list.
['Rice', 'Bread', 'Milk']Challenge 3: Highest Score
A list of 5 scores: [88, 72, 95, 60, 84]. Print the highest and lowest.
Highest: 95Lowest: 60Challenge 4: Remove Duplicates
Cities visited: ['Oslo', 'London', 'Oslo', 'Nairobi', 'London']. Put them in a set and print the unique cities.
Unique: {'Oslo', 'London', 'Nairobi'}Challenge 5: Student Card
Create a dictionary for a student: name, age, city. Print each value.
Name: AhmedAge: 22City: OsloChallenge 6: Update Profile
Same student dictionary. Update the city to "London". Add an email. Print all values.
Name: AhmedAge: 22City: LondonEmail: ahmed@email.comChallenge 7: Phone Book
Create a dictionary with 3 contacts (name: phone). Ask the user for a name. If found, print the number. If not, print "Not found."
Search: AhmedAhmed: 98765432Challenge 8: Shopping List
Ask the user to add items one by one. Type "done" to stop. Print the final list and total items.
Add item: MilkAdd item: BreadAdd item: EggsAdd item: done
1. Milk2. Bread3. EggsTotal: 3Challenge 9: Class Results
Given this list of students:
students = [ {"name": "Ahmed", "score": 85}, {"name": "Bile", "score": 45}, {"name": "Nimo", "score": 67}]Pass is 60. Print pass or fail for each.
Ahmed: 85 - PassBile: 45 - FailNimo: 67 - PassChallenge 10: Product Lookup
Given this list of products:
products = [ {"name": "Laptop", "price": 999}, {"name": "Phone", "price": 699}, {"name": "Headphones", "price": 149}]Ask the user for a product. If found, print the price. If not, print "Not found."
Search: LaptopLaptop: $999Search: TabletNot found.