Program 1
Create & access dict
Basic dictionary operations.
python
student = {"name": "Riya", "age": 21, "grade": "A"}
print(student["name"])
student["age"] = 22
student["city"] = "Mumbai"
print(student)Topic 08 of 17
Key-value mappings — fast lookups and structured data.
Program 1
Basic dictionary operations.
student = {"name": "Riya", "age": 21, "grade": "A"}
print(student["name"])
student["age"] = 22
student["city"] = "Mumbai"
print(student)Program 2
Walk keys and values together.
prices = {"apple": 30, "banana": 10, "mango": 50}
for item, price in prices.items():
print(f"{item}: ₹{price}")Program 1
Build a dict in a single expression.
squares = {x: x * x for x in range(1, 6)}
print(squares)Program 2
Use the | operator (Python 3.9+).
a = {"x": 1, "y": 2}
b = {"y": 20, "z": 3}
print(a | b)Program 1
Group items using defaultdict.
from collections import defaultdict
people = [("dev", "Asha"), ("design", "Ravi"), ("dev", "Maya"), ("design", "Sam")]
teams = defaultdict(list)
for team, name in people:
teams[team].append(name)
print(dict(teams))