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

Instruction

Good job! It's time to wrap things up. Before we do this section's quiz, let's have a quick review of what we've covered:

  1. To iterate over dictionary keys, use either:
    for k in dictionary:

    or:

    for k in dictionary.keys():
  2. To iterate over dictionary values, use:
    for v in dictionary.values():
  3. To get both values and keys during dictionary iteration, use:
    for k, v in dictionary.items():
  4. Dictionaries are often used to count elements:
    votes = [...]
    dict_results = {}
    for vote in votes:
      if vote not in dict_results:
        dict_results[vote] = 0
      dict_results[vote] += 1
  5. The syntax above can be simplified to:
    votes = [...]
    dict_results = {}
    for vote in votes:
      dict_results[vote] += dict_results.get(vote, 0) + 1
  6. Dictionaries are also used to group elements by a given characteristic, for instance:
    names = [...]
    groups_dict = {}
    for name in names:
      key = len(name)
      if key not in groups_dict:
        groups_dict[key] = []
      groups_dict[key].append(name)
  7. You can update the values in a dictionary based on another dictionary using:
    dict_1.update(dict_2)
  8. Great! Are you ready for the quiz?

Exercise

Click Next exercise to continue.