SECTION 01 / 31

A dictionary connects a key to a value

1 min read

A dictionary is a collection of labelled entries. Each entry has a KEY, which tells us what to look for, and a VALUE, which is the information stored there. Think of a labelled drawer: give its label and get what is inside. You do not need to walk through all earlier drawers.

A CardNo key points to one Chemistry mark. Actual course entries include 7 → 76 and 21 → 74.
A CardNo key points to one Chemistry mark. Actual course entries include 7 → 76 and 21 → 74.

Write a colon between each key and its value, and curly braces around the dictionary: {7:76, 21:74}. The key 7 identifies Aditya; the value 76 is Aditya’s Chemistry mark. Monika has CardNo 21 and Chemistry 74. A key need not be a student ID: city names, customers, birthdays and train numbers are useful keys too.

QuestionKeyValue
What is this student’s mark?CardNoMark
How many students are from this city?CityCount
Who has this birthday?Day + monthList of IDs
Where does this historical train start?Train number stringSource station

Try it yourself

In {7:76,21:74}, what are the keys?

Show explained answer

7 and 21. The values are 76 and 74.

Try it yourself

Can a dictionary value be a list?

Show explained answer

Yes. A value can be a number, Boolean, string, list or another dictionary, depending on the problem.

Try it yourself

Is a dictionary just a list of student IDs?

Show explained answer

No. Its entries connect each unique key to a value. A list is an ordered sequence of elements.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑