{"id":"dictionaries-basics","slug":"basics","title":"Keys and values","text":"A list finds its items by position: 0, 1, 2 and so on. Often you'd rather find a value by a name: the price of a coffee, the phone number of Ada. A **dictionary** stores pairs of a **key** and a **value**, and you find each value by its key, the way you look up a word in a real dictionary to find what it means.\n\n```python\nprices = {}\nprices[\"coffee\"] = 2.5\nprices[\"tea\"] = 2.0\nprices[\"cake\"] = 3.75\n\nprint(prices)\nprint(len(prices))\nprint(prices[\"tea\"])\n```\n\n```output\n{'coffee': 2.5, 'tea': 2.0, 'cake': 3.75}\n3\n2.0\n```\n\n- `{}` is an empty dictionary.\n- `prices[\"coffee\"] = 2.5` adds the key `\"coffee\"` with the value 2.5.\n- `prices[\"tea\"]`, with a key in square brackets, gives that key's value.\n- `len` counts the pairs, and `print` shows them all, in curly braces, with a colon between each key and its value.\n\nYou can also write the pairs right away, in that same form:\n\n```pycon\n>>> capitals = {\"Finland\": \"Helsinki\", \"Peru\": \"Lima\"}\n>>> capitals[\"Peru\"]\n'Lima'\n>>> capitals[\"Chile\"] = \"Santiago\"\n>>> capitals\n{'Finland': 'Helsinki', 'Peru': 'Lima', 'Chile': 'Santiago'}\n>>> type(capitals)\n<class 'dict'>\n```\n\nPython's name for the dictionary type is `dict`.\n\n### Is the key there?\n\nAsking for a key that isn't in the dictionary stops the program with a `KeyError`, which names the missing key:\n\n```python\nprices = {\"coffee\": 2.5, \"tea\": 2.0}\nprint(prices[\"juice\"])\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(prices[\"juice\"])\n          ~~~~~~^^^^^^^^^\nKeyError: 'juice'\n```\n\nThe `in` operator checks first. With a dictionary, it looks at the keys only, not at the values:\n\n```pycon\n>>> prices = {\"coffee\": 2.5, \"tea\": 2.0}\n>>> \"tea\" in prices\nTrue\n>>> 2.5 in prices\nFalse\n```\n\nTogether, `in` and a lookup make a program that finds things for the user:\n\n```python\nprices = {\"coffee\": 2.5, \"tea\": 2.0, \"cake\": 3.75}\n\nitem = input(\"What would you like? \")\nif item in prices:\n    print(f\"That's {prices[item]:.2f} euros.\")\nelse:\n    print(\"Sorry, we don't have that.\")\n```\n\n```input\ncake\n```\n\n```output\nWhat would you like? cake\nThat's 3.75 euros.\n```\n","task":"Fill in the blanks so the program adds a spider, with 8 legs, to the dictionary. Then it prints the number of animals in the dictionary and the legs of a bird and a spider together:\n\n```\n3\n10\n```\n","kind":"fill","seed":"legs = {\"cat\": 4, \"bird\": 2}\nlegs[[[\"spider\"‖'spider']]] = 8\nprint(len(legs))\nprint(legs[[[\"bird\"‖'bird']]] + legs[\"spider\"])\n","solution":"legs = {\"cat\": 4, \"bird\": 2}\nlegs[\"spider\"] = 8\nprint(len(legs))\nprint(legs[\"bird\"] + legs[\"spider\"])\n","hints":["A key goes in square brackets after the dictionary's name, written as you would write the string anywhere else: `\"spider\"`, with quotes.","The first blank is the new key `\"spider\"`, and the second is the key whose value is the bird's legs, `\"bird\"`."],"tests":[{"name":"Output","stdin":"","expect":"3\n10","hidden":false}],"require":[],"forbid":[],"more":[{"task":"A cinema keeps its showtimes in a dictionary. Complete the program: it asks for a movie and prints when it starts, or `No such movie today` when the movie isn't in the dictionary. Use the prompt `Movie: ` exactly. An example run:\n\n```\nMovie: Tiny Giants\nTiny Giants starts at 16:15\n```\n","kind":"code","seed":"showtimes = {\"Space Dogs\": \"18:00\", \"The Last Lighthouse\": \"20:30\", \"Tiny Giants\": \"16:15\"}\n","solution":"showtimes = {\"Space Dogs\": \"18:00\", \"The Last Lighthouse\": \"20:30\", \"Tiny Giants\": \"16:15\"}\n\nmovie = input(\"Movie: \")\nif movie in showtimes:\n    print(f\"{movie} starts at {showtimes[movie]}\")\nelse:\n    print(\"No such movie today\")\n","hints":["Read the movie's name with `input(\"Movie: \")`. Then check whether it's a key of `showtimes` with `in`.","If it is, `showtimes[movie]` is its starting time. Put both in an f-string: `f\"{movie} starts at {showtimes[movie]}\"`.","Without the `in` check, a movie that isn't in the dictionary would stop the program with a `KeyError`."],"tests":[{"name":"Test 1","stdin":"Tiny Giants\n","expect":"Movie: Tiny Giants\nTiny Giants starts at 16:15","hidden":false},{"name":"Test 2","stdin":"Frozen Seas\n","expect":"Movie: Frozen Seas\nNo such movie today","hidden":false},{"name":"Test 3","stdin":"The Last Lighthouse\n","expect":"Movie: The Last Lighthouse\nThe Last Lighthouse starts at 20:30","hidden":true},{"name":"Test 4","stdin":"space dogs\n","expect":"Movie: space dogs\nNo such movie today","hidden":true}],"require":[],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"stock = {\"apples\": 5, \"pears\": 0}\nstock[\"plums\"] = 7\nprint(len(stock))\nprint(stock[\"apples\"] + stock[\"plums\"])\nprint(\"pears\" in stock)\nprint(5 in stock)\n","solution":"stock = {\"apples\": 5, \"pears\": 0}\nstock[\"plums\"] = 7\nprint(len(stock))\nprint(stock[\"apples\"] + stock[\"plums\"])\nprint(\"pears\" in stock)\nprint(5 in stock)\n","hints":["After the second line, the dictionary has three keys.","`stock[\"apples\"]` is 5 and `stock[\"plums\"]` is 7.","`in` checks the keys only. A key with the value 0 is still a key, and 5 is a value, not a key."],"tests":[{"name":"Output","stdin":"","expect":"3\n12\nTrue\nFalse","hidden":false}],"require":[],"forbid":[],"lines":["3","12","True","False"]}]}