ALL ABOUT DICTIONARIES
15 Apr 2020
Last updated
15 Apr 2020
Last updated
In this article, we will explore Python dictionaries. For my previous tutorials on Python data structures, please see the below links.
A dictionary is one of Python’s primary data structures. In essence, a dictionary (also shortened as dict) is a collection of key-value pairs, similar to an actual dictionary (like the one pictured above). When you look up a word in a dictionary, you find a corresponding definition. Similarly, in a Python dictionary, each key has a corresponding value.
In dictionaries, one key can have only one value. However, the value can be anything from a single number or character, to lists, tuples, other dictionaries, or even functions!
Dictionaries can be created several ways. The most straightforward way is below using curly braces.
In this example, we create a dictionary with three elements – that is, three key-value pairs. The keys are “a”, “b”, and “c”, while the corresponding values are 1, 2, and 3.
We can get the keys of our dictionary using the keys method.
Similarly, we can get the values of a dictionary using the values method.
We can add new key-value pairs to a dictionary using bracket notation.
Likewise, if we want to refine a key-value pair, we also use bracket notation.
Key-value pairs can be removed in a couple of different ways. The first is using the pop method.
Using the pop method both removes the key-value pair and returns the value corresponding to the key input into the method. Alternatively, we can remove a key-value pair using Python’s del method. This way does not return any value.
Dictionaries can be combined using the update method. For example, we currently have:
mapper = {“a”, “b”, “c”}
Now let’s create another dictionary called extra:
Next, let’s combine mapper and extra.
Running the update method here will update mapper to also have the key-value pairs in extra.
We can convert a dictionary into a list of tuples using the items method. Here, each tuple will be a key-value pair. The first line below will create a dict_items object, which is iterable, so we can loop over the key-value pairs in this object. However, this object is not indexable – i.e. trying to run mapper.items()[0] will result in an error. However, if we wanted to reference key-value pairs by index, we can convert this to a list. In practice, we usually would not need to do this conversion.
The items method is especially useful when iterating over a dictionary, or when creating a dictionary comprehension, which we’ll discuss next.
Suppose we have dictionary of filenames – each key is a filename, and each value is a new name that we want to give for the key. Here, we can loop over the items (key-value pairs) in the dictionary and change each file name.
Similar to list comprehensions, we can also create dictionaries using dictionary comprehensions. We can do that using the items method we just covered. A dictionary comprehension is a way of creating a dict by looping over the keys and values of an existing dict – just like how a list comprehension creates a new list by looping over the elements of an existing list.
Dictionary comprehensions also support if-else statements, like below. Again, this is similar to list comprehensions.
As mentioned earlier in this post, dictionaries can contain many types of objects. Let’s create a dictionary with lists as some of its values.
We can also create dicts with functions as values.
Creating dictionaries in this way is a alternative to organizing functions. In addition to having functions as values, we can also have functions as keys!
Reference : http://theautomatic.net/2020/04/15/all-about-python-dictionaries/