Course 1.1Lesson 1 of 5
A dictionary stores pairs of a key and a value, and finds a value by its key. It is written in curly brackets, with key: value pairs separated by commas. Look up a value with d[key], count the pairs with len, and ask whether a key is there with in, which asks about the keys and not the values. A key must be unique and fixed, such as text, a number or a tuple. A key that is not in the dictionary gives a KeyError.
Try the code and the simulations beside the lesson.
After this lesson you can
KeyError and a TypeError about keys.A list finds things by position: item 0, item 1, and so on. But what if you want the phone number of Ravi? You would like to ask for "Ravi", and not for position 7. You would have to remember where Ravi is in the list.
A dictionary works like a real dictionary. You look up a word, and you get its meaning. Python's dictionary stores pairs:
Think of a cupboard with labelled drawers. The label on the drawer is the key, and what is inside is the value. You never count the drawers. You read the label.
A dictionary is written in curly brackets. Each pair is key: value, and the pairs are separated by commas.
Predict first. Before you run it, write down what you think each line shows. Then run it and compare.
ages["Ravi"] looks up the key "Ravi" and gives 13.len(ages) counts the pairs: 3."Meera" in ages asks: is "Meera" a key? Yes, so True. And "Kabir" in ages is False.12 in ages is False, although the value 12 is there. The word in asks about the keys, never the values. This surprises many learners. To ask about the values, use 12 in ages.values().type(ages) is dict, the sixth type of value that you know.{} is an empty dictionary. Do not confuse it with an empty list [].For a longer dictionary, write one pair on each line. A comma after the last pair is allowed, and makes it easy to add one more.
The values can be anything: numbers, text, lists, even other dictionaries (a later lesson). The keys must follow two rules.
And what if the key is a list?
The words under the bars are the keys. Asking for a key that is not there is the usual mistake, so check with in or use get().
| You have | Use |
|---|---|
| items in an order, found by position | a list |
| items found by a name, where the order does not matter | a dictionary |
| a few values that belong to one thing | a dictionary: {"name": "Asha", "age": 12} |
In a dictionary, finding a key is fast, even for a million pairs. Python does not look through them one by one. In a list, the word in looks at the items one by one.
[ ] instead of { } to make the dictionary.KeyError.value in d and expecting it to look at the values.TypeError.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
{key: value} pairs. Look up with d[key].len(d) counts the pairs. key in d asks about the keys.KeyError.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
{"a": 1, "b": 2}["b"]?{"a": 1}["z"] do?1 in {"a": 1}?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.