Python Learning Series · @onlysamarrr · Day 18 / 30
Day 18 covers dict methods: keys() gives all keys, values() gives all values, and items() gives (key, value) pairs. Never add or delete keys while looping over a dict, or you get a RuntimeError.
Phase: Intermediate · Reels: 86/450 – 90/450
Day 18 covers dict methods: keys() gives all keys, values() gives all values, and items() gives (key, value) pairs. Never add or delete keys while looping over a dict, or you get a RuntimeError.
marks = {"Arhan": 92, "Riya": 85, "Aman": 78}
print(list(marks.keys()))
print(list(marks.values()))
print(list(marks.items()))
count = {}
for w in "to be or not to be".split():
count[w] = count.get(w, 0) + 1
print(count)
print(count["to"])
['Arhan', 'Riya', 'Aman']
[92, 85, 78]
[('Arhan', 92), ('Riya', 85), ('Aman', 78)]
{'to': 2, 'be': 2, 'or': 1, 'not': 1}
2
Word counts, vote counts and frequency tables all use this dict pattern.
Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.
Q1. What does .items() return?
Q2. What happens if you add a key while looping over a dict?
Q3. Do dicts remember insertion order in modern Python?
| 86/450 | Concept | keys, values, items |
| 87/450 | Live demo | get() aur update() demo |
| 88/450 | Common mistake | Loop mein dict change mat karo |
| 89/450 | Quiz | Quiz: word count |
| 90/450 | Interview / real life | Interview: dict ordered hai? |