Lists and Dictionaries
Learn list mutation (.append(), .pop()) and dictionary key-value operations.
Learning objectives
- Create and modify lists with methods like append(), extend(), and pop()
- Build dictionaries, access values by key, and iterate using .items()
Lesson material
Lists (Mutable Sequences)
Lists are ordered, mutable collections defined with square brackets [].
Example code
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
fruits.pop(0)
print(fruits)
Dictionaries (Key-Value Pairs)
Dictionaries map unique hashable keys to arbitrary values using curly braces {}.
Example code
student = {"name": "Alex", "age": 22, "grade": "A"}
print("Name:", student["name"])
student["age"] = 23
print("Updated Age:", student["age"])
Practice exercise: Dictionary Lookup and List Mutation
Create a list `nums = [10, 20, 30]`, append `40` to it, and print the sum of all numbers using sum(nums).
Test yourself with the Module 4: Collections (Lists, Tuples, Sets, Dicts) quiz →