Python for Beginners 20: Dictionaries

This final lesson of the Python for Beginners series deals with another sequence-based data type: dictionaries. Allen B. Downey from Think Python even calls it “one of Python’s best features”.

Dictionaries are basically lists with the feature that each value is not referenced by an integer index, but by a key that can be of any (immutable) data type. As there is now a nearly infinite number of potential keys, we need to specify them when entering new values and each key must be associated with a single value. This association is called a key-value pair or sometimes an item. We say that each key “maps to” a value.

A dictionary is surrounded by squiggly brackets {}, with the individual key-value pairs being separated by commas, just like lists. Every key-value pair has a colon between the key and value. To create a new dictionary, you can just follow this syntax. Alternatively, you can create an empty one using the function dict() and then add items afterwards.

Adding items to a dictionary works just like replacing elements in a list:
myList[5] = ‘green’
myDict[‘one’] = ‘uno’

The difference is, of course, as previously mentioned, that you use a key instead of an index. What might be a bit surprising, is the fact that there is no fixed order in a dictionary. Every time you print it, the order may be different. However, as we use keys to reference the values, this does not constitute a problem.

Operations with sequence-based data types that work with dictionaries

  • the function len() returns the number of key-value pairs
  • the in operator returns True if something appears as a key in the dictionary
  • the function sorted() returns the key-value pairs in (numerical/alphabetical) order

Methods that only work with dictionaries

  • .get(key, default value): If the key appears in the dictionary, it returns the corresponding value; otherwise it returns the default value.
  • .values(): returns a collection of all the values that appear in a dictionary (you can combine it with the in operator to see whether something appears as a value in a dictionary)

When to use a dictionary?

Besides the obvious application (use a dictionary when you want to create a dictionary), there are some other cases when using the data type dictionary can be handy.

  • If you want to count several elements, for instance how often a letter appears within a sentence, you can create a dictionary with characters as keys and counters as the corresponding values.
  • Unlike lists, Python dictionaries use a data structure called a hashtable. Thus, whereas searching takes more and more time as lists grow longer, there is basically no change for dictionaries, independently from how many items they contain.
  • To improve the speed of your program, you can also store previously calculated values in dictionaries as memos.

If you want to learn more about dictionaries or about learning programming with Python, I recommend once again Allen B. Downey’s Think Python, which also inspired this post.
#mastery20

Leave a comment