Example: A dictionary with cities located in the US and Canada and their corresponding population. Taken those numbers out the "List of North American cities by population" from Wikipedia (https://en.wikipedia.org/wiki/List_of_North_American_cities_by_population)
>>> city_population = {"New York City":8550405, "Los Angeles":3971883, "Toronto":2731571, "Chicago":2720546, "Houston":2296224, "Montreal":1704694, "Calgary":1239220, "Vancouver":631486, "Boston":667137}
If we want to get the population of one of those cities, all we have to do is to use the name of the city as an index:
>>> city_population["New York City"] 8550405 >>> city_population["Toronto"] 2731571 >>> city_population["Boston"] 667137
What happens, if we try to access a key, i.e. a city, which is not contained in the dictionary? We raise a KeyError:
>>> city_population["Detroit"] Traceback (most recent call last): File "<stdin>", line 1, in KeyError: 'Detroit' >>>