A dictionary stores pairs of a key and a value, such as a student name and a score. You look up a value by its key, which is faster and clearer than searching a list.
Creating and using one
- Create it with curly brackets: scores = {“Asha”: 82, “Ravi”: 74}
- Read a value: scores[“Asha”] returns 82.
- Add or change: scores[“Meena”] = 90
- Loop through pairs: for name, score in scores.items()
When to use one
Use a dictionary to count things, map codes to names, or hold one record with several fields. For example, a dictionary can store one order with its date, customer and amount.
Read Python basics: variables, lists and loops first, then list comprehensions. Book a free demo class and ask a counsellor about the Python course.
