Session 9 · Dictionaries: Annachi Kadai#
📖 Based on the blog post Task: Annachi Kadai, Python Dictionary.
The big idea#
A dictionary stores key → value pairs. Instead of asking “what’s in position 2?”, you ask “what’s the price of dal?”. Lookups by key are instant, even with millions of entries.
Everyday example: the annachi kadai (the corner grocery shop). Annachi doesn’t search shelf by shelf; he knows rice → ₹60, dal → ₹120. The item name is the key, the price is the value.
Creating and reading#
prices = {"rice": 60, "dal": 120, "oil": 180}
print(prices["dal"])
print(len(prices))
print("oil" in prices, "tea" in prices) # `in` checks the KEYS
120
3
True False
Asking for a missing key with [] raises KeyError. get() returns a default instead:
prices = {"rice": 60, "dal": 120}
print(prices.get("tea")) # None
print(prices.get("tea", 0)) # your default
None
0
Adding, updating, removing#
prices = {"rice": 60, "dal": 120, "oil": 180}
prices["sugar"] = 45 # new key → added
prices["dal"] = 125 # existing key → updated
del prices["oil"] # removed
removed = prices.pop("rice") # removed, and you get the value back
print(prices, removed)
{'dal': 125, 'sugar': 45} 60
Keys are unique: assigning to a key that exists replaces its value. Keys must be immutable (strings, numbers, tuples); values can be anything, even lists or other dictionaries.
Looping#
prices = {"rice": 60, "dal": 120}
for item, price in prices.items():
print(f"{item:>5}: ₹{price}")
print(list(prices.keys()), list(prices.values()))
rice: ₹60
dal: ₹120
['rice', 'dal'] [60, 120]
Since Python 3.7, a dictionary remembers the order you inserted keys.
Counting things: a classic pattern#
fruits = ["apple", "banana", "apple", "orange", "banana", "banana"]
count = {}
for fruit in fruits:
count[fruit] = count.get(fruit, 0) + 1
print(count)
{'apple': 2, 'banana': 3, 'orange': 1}
Common mistakes#
d["missing"]raisesKeyError. Useget()or checkinfirst.Lists as keys:
{[1, 2]: "x"}raisesTypeError(unhashable). Use a tuple.Changing size while looping (
for k in d: del d[k]) raisesRuntimeError. Loop overlist(d)instead.copy()is shallow: nested dictionaries inside are still shared. Usecopy.deepcopy()for those.
Hands-on exercises#
The tasks from Task: Annachi Kadai. Each solution sets up what it needs so you can run it alone.
Task 1. Create student with name “Alice”, age 21, major “Computer Science” and print it.
Solution
student = {"name": "Alice", "age": 21, "major": "Computer Science"}
print(student)
Task 2. Print the values for "name" and "major".
Solution
student = {"name": "Alice", "age": 21, "major": "Computer Science"}
print(student["name"], student["major"])
Task 3. Add "gpa": 3.8, then update "age" to 22.
Solution
student = {"name": "Alice", "age": 21, "major": "Computer Science"}
student["gpa"] = 3.8
student["age"] = 22
print(student)
Task 4. Remove "major" with del and print the dictionary.
Solution
student = {"name": "Alice", "age": 22, "major": "Computer Science"}
del student["major"]
print(student)
Task 5. Check whether the key "age" exists.
Solution
student = {"name": "Alice", "age": 22}
print("age" in student) # True
Task 6. Create prices = {"apple": 0.5, "banana": 0.3, "orange": 0.7} and print each pair.
Solution
prices = {"apple": 0.5, "banana": 0.3, "orange": 0.7}
for fruit, price in prices.items():
print(fruit, price)
Task 7. Print how many pairs prices has.
Solution
prices = {"apple": 0.5, "banana": 0.3, "orange": 0.7}
print(len(prices)) # 3
Task 8. Use get() for "gpa", and for "graduation_year" with a default of 2025.
Solution
student = {"name": "Alice", "gpa": 3.8}
print(student.get("gpa")) # 3.8
print(student.get("graduation_year", 2025)) # 2025 (key doesn't exist)
Task 9. Merge extra_info = {"graduation_year": 2025, "hometown": "Springfield"} into
student with update().
Solution
student = {"name": "Alice", "gpa": 3.8}
extra_info = {"graduation_year": 2025, "hometown": "Springfield"}
student.update(extra_info)
print(student)
Task 10. Use a dictionary comprehension to map 1–5 to their squares.
Solution
squares = {n: n ** 2 for n in range(1, 6)}
print(squares) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Task 11. Print the keys and values of prices as two separate lists.
Solution
prices = {"apple": 0.5, "banana": 0.3, "orange": 0.7}
print(list(prices.keys()))
print(list(prices.values()))
Task 12. Create school with two nested students and print the age of "student2".
Solution
school = {
"student1": {"name": "Alice", "age": 21},
"student2": {"name": "Bob", "age": 22},
}
print(school["student2"]["age"]) # 22
Task 13. Use setdefault() to add "advisor": "Dr. Smith" only if it’s missing.
Solution
student = {"name": "Alice"}
student.setdefault("advisor", "Dr. Smith")
student.setdefault("advisor", "Dr. Jones") # ignored: the key exists now
print(student) # {'name': 'Alice', 'advisor': 'Dr. Smith'}
Task 14. pop() the "hometown" key into a variable and print it.
Solution
student = {"name": "Alice", "hometown": "Springfield"}
hometown = student.pop("hometown")
print(hometown, student)
Task 15. Empty prices with clear().
Solution
prices = {"apple": 0.5, "banana": 0.3}
prices.clear()
print(prices) # {}
Task 16. copy() the student, change the copy’s name to “Charlie”, print both.
Solution
student = {"name": "Alice", "age": 22}
clone = student.copy()
clone["name"] = "Charlie"
print(student) # {'name': 'Alice', 'age': 22}
print(clone) # {'name': 'Charlie', 'age': 22}
Task 17. Build a dictionary from keys = ["name", "age", "major"] and
values = ["Eve", 20, "Mathematics"] with zip().
Solution
keys = ["name", "age", "major"]
values = ["Eve", 20, "Mathematics"]
print(dict(zip(keys, values)))
Task 18. Loop over student with items().
Solution
student = {"name": "Eve", "age": 20}
for key, value in student.items():
print(f"{key}: {value}")
Task 19. Count the fruits in ["apple", "banana", "apple", "orange", "banana", "banana"].
Solution
fruits = ["apple", "banana", "apple", "orange", "banana", "banana"]
fruit_count = {}
for fruit in fruits:
fruit_count[fruit] = fruit_count.get(fruit, 0) + 1
print(fruit_count) # {'apple': 2, 'banana': 3, 'orange': 1}
Task 20. Count words with collections.defaultdict.
Solution
from collections import defaultdict
word_count = defaultdict(int) # missing keys start at int() = 0
for word in ["hello", "world", "hello", "python"]:
word_count[word] += 1
print(dict(word_count)) # {'hello': 2, 'world': 1, 'python': 1}
collections.Counter(words) does the same in one line.