Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Recap
2. Recap
Counting with dictionaries
Grouping with dictionaries
Linking with dictionaries
Summary

Instruction

Before we start discussing the use of dictionaries, let's do a quick review of their basics.

dictionary

In Python, dictionaries are data structures that map keys to values. Dictionaries can contain values of any type, but the keys must be immutable – strings, integers, or even tuples are allowed, but lists aren't. The elements in dictionaries are unordered – there is no "first" or "last" element.

Here are the basic dictionary operations:

  1. Define a dictionary:
  2. scores = {'John': 234, 'Mary': 984, 'Kate': 453}
  3. Access an element with the 'Kate' key:
  4. scores['Kate']
  5. Update a value:
  6. scores['John'] = 237
  7. Add a new element with the 'Alfred' key:
  8. scores['Alfred'] = 354
  9. Delete an element:
  10. del scores['John']

Exercise

We've created a dictionary named movies that stores the number of movies John saw last year. As you see, John saw no biography movies, so we don't need this kind of movie in our dictionary.
Remove the 'biography' record from the movies dictionary.

We also noticed that we don't have 'western' movies in the dictionary. John saw 3 Westerns last year.
Add this record to the dictionary.