Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dictionary basics
2. Dictionaries – theory
Dictionaries in loops and conditional statements
Dictionaries in functions
Summary

Instruction

Good. It's best to explain what dictionaries are by comparing them with lists. As you know, lists are collections of values in a given order. For instance:

daily_sales = [4324, 4634, 5432, 3834]

In the list above, 4324 is the first element 4634 the second element, etc. We can access each element with its index: daily_sales[0], daily_sales[1], etc. Indices are automatically created for elements based on their positions in the list.

In dictionaries, we use keys instead of indices. We can say that dictionaries map keys to values. Keys can be identifiers of various types: integers, doubles, strings, etc. Keys are not created automatically like indices are in lists – you need to specify them on your own. For instance:

phonebook = {'Mary': '8349374', 'Anne': '7439834', 'John': '3472983'}

The dictionary above is a phone book that maps names to phone numbers. 'Mary' is a key (identifier), and '8349374' is a value associated with that key. Note that dictionaries are unordered – this means that there is no single 'order' in which keys are stored internally in the dictionary.

Exercise

Click Next exercise to continue.