Course 1.1Lesson 2 of 5
You change a dictionary by writing to a key: d[key] = value adds a pair if the key is new and replaces the value if it exists. del d[key] and d.pop(key) remove a pair, and pop gives the value back. d.get(key, default) looks up safely and never gives a KeyError. keys, values and items show the whole dictionary, and sum, max and sorted work on them. update adds the pairs of another dictionary.
Try the code and the simulations beside the lesson.
After this lesson you can
KeyError,sum, max and sorted on a dictionary.Unlike a string, a dictionary can change, as a list can. You change it by writing to a key.
d["Meera"] = 64 adds a new pair if "Meera" is not a key yet.d["Asha"] = 78 replaces the value if "Asha" is already a key.del d["Ravi"] removes a pair.d.pop("Meera") also removes a pair, and gives the value back.Predict first, and pay attention to the last lines. Then run it.
del, Ravi is gone.marks.pop("Meera") removes Meera and gives back her mark, 64, which we store in gone.The same line, d[key] = value, both adds and changes. Python decides by asking: is the key already there? This is useful: you do not need to check.
You saw that marks["Kabir"] stops with a KeyError if Kabir is not a key. The method get is safe.
Which one to use? get is shorter when you have a sensible default, such as 0 for a count. The if ... in form is better when the two cases need different actions, as in a message.
| Call | Gives |
|---|---|
d.keys() | all the keys |
d.values() | all the values |
d.items() | the pairs, each as (key, value) |
The same line, stock[key] = value, does both jobs: it changes the value of a key that exists, and adds a key that does not.
A dictionary remembers the order in which the keys were added. So the output of print(marks) shows the pairs in the order of adding, and a new pair goes at the end. But do not rely on the order to find things. You find things by key. If you need a particular order, use sorted.
d[key] when the key may be missing. A KeyError. Use get or in.d[key] = value replaces. An old value is lost without a message.del d[key] for a missing key. A KeyError. Use d.pop(key, None) if it may be missing.RuntimeError. Make a list of the keys first: for key in list(d):.d.keys() where a list is needed. Wrap it in list().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:
d[key] = value adds a new pair or replaces the old value.del d[key] removes. d.pop(key) removes and gives the value back.d.get(key, default) never gives a KeyError.keys(), values() and items() show the whole dictionary. sorted(d) gives the keys in order.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
d = {"a": 1} and d["b"] = 2, what is len(d)?{"a": 1}.get("z", 5) give?d["a"] = 9 do when "a" is already a key?d?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.