A brief note on Dictionaries, Tuples and Sets: looking up and changing pairs, counting and grouping with a dictionary, fixed tuples and unpacking, sets and their operations, and choosing a container. With a worked example, practice, a program and questions to check yourself.
A brief note on Dictionaries, Tuples and Sets. Read it before a test. Try each question at the end before you open its answer.
key: value pairs: fees = {"chess": 300, "dance": 450}. Look up with fees["dance"].len(fees) counts the pairs. "chess" in fees asks about the keys, not the values.fees["art"] = 200 adds a new pair, or replaces the old value if the key is already there.del fees["art"] removes a pair. fees.pop("art") removes it and gives the value back.fees.get("drama", 0) gives 0 for a missing key, instead of a KeyError.keys(), values() and items() show the whole dictionary. for key, value in fees.items(): gives both.counts[x] = counts.get(x, 0) + 1. The key is the thing you count; the value is how many times.append.pupils["Zoya"]["marks"][0].seat = (2, 5). It cannot be changed after it is made.row, column = seat. Swapping: a, b = b, a.return two values as a tuple, and the caller unpacks them.(2, 5) can stand for row 2, column 5.set(names) removes the repeats.add puts a value in; discard takes one out, with no error if it is missing. x in s is a quick test.set() is an empty set. {} is an empty dictionary.A and B: A & B is in both, A | B is in either, A - B is only in A, and A ^ B is in exactly one.A - B and B - A are different. Use sorted(s) to show a set in order.| Container | Order | Can change | Repeats | Found by |
|---|---|---|---|---|
| list | yes | yes | yes | position |
| tuple | yes | no | yes | position |
| set | no | yes | no | in |
| dict | yes | yes | keys no | key |
Ask three questions: position or name? Fixed or changing? Do the order and the repeats matter?
A class votes for the fruit of the school fair. What does this program show?
Follow the dictionary after each vote.
| Vote | fruit | counts after the vote |
|---|---|---|
| 1 | mango | {'mango': 1} |
| 2 | apple | {'mango': 1, 'apple': 1} |
| 3 | mango | {'mango': 2, 'apple': 1} |
| 4 | kiwi | {'mango': 2, 'apple': 1, 'kiwi': 1} |
| 5 | mango | {'mango': 3, 'apple': 1, 'kiwi': 1} |
| 6 | apple | {'mango': 3, 'apple': 2, 'kiwi': 1} |
counts.get(fruit, 0) gives 0, so the new key starts at 1. No KeyError happens.get gives the old count, and the line stores one more.The program shows {'mango': 3, 'apple': 2, 'kiwi': 1}.
A reading club notes each book that a member reads, as a tuple of a name and a book. The program groups the books of each reader in a set, counts the readings of each book, and finds the book read most.
fees["drama"] stops with KeyError: 'drama'. Check with in, or use get with a default.seats[[2, 5]] = "Imran" stops with TypeError: unhashable type: 'list'. Use the tuple (2, 5).seat[0] = 3 stops with TypeError: 'tuple' object does not support item assignment.{} for an empty set. It makes an empty dictionary. Write set().colours[0] stops with TypeError: 'set' object is not subscriptable. A set has no order.in for a value. in looks at the keys of a dictionary. To search the values, use value in d.values().stock = {"pens": 40, "files": 12}. It receives 15 more files. Write one line that updates the stock, and give the new number of files.[2, 5] or (2, 5), and why?[4, 7, 4, 9, 7, 7]. Which short expression gives the number of different pupils who handed in, and what is it?empty = {} and then empty.add(5). Python stops with AttributeError: 'dict' object has no attribute 'add'. What went wrong?Log in to join the discussion.
Loading…