DictionariesΒΆ

As an example, we will create a dictionary to translate English words into Spanish. For this dictionary, the keys are strings and the values will also be strings.

One way to create a dictionary is to start with the empty dictionary and add key-value pairs. The empty dictionary is denoted {}

(chp12_dict1)

Another way to create a dictionary is to provide a list of key-value pairs using the same syntax as the previous output.

(chp12_dict2)

It doesn’t matter what order we write the pairs. The values in a dictionary are accessed with keys, not with indices, so there is no need to care about ordering.

Here is how we use a key to look up the corresponding value.

(chp12_dict3)

The key 'two' yields the value 'dos'.

Next Section - Dictionary Operations