In a python dictionary, an association is formed between what two components of the dictionary

Photo by Joshua Hoehne on Unsplash

After Lists, Sets and Tuples, dictionaries are the next inbuilt data structure that comes ready in Python. They are commonly used in programming and lays the foundation for more advanced structures and functionality in Python within many different libraries. They take the form similar to an actual dictionary where you have keys (words) that have values (descriptions) and thus take a key:value structure. Their main characteristics are that:

  • Mutable: They can be changed once they have been defined
  • Ordered: They maintain their order unless explicitly changed
  • Indexable: We can access specific items if we know their position (their key) in the dictionary
  • No duplicate keys: They cannot contain duplicates in key values, although they can in the values.

The important part of dictionaries is their key-value structure meaning that instead of accessing items using a numerical index as in Lists or Tuples, we use their key to access their value. This then allows the dictionary to be used when we may want to access records using a specific key, such as sales records or a login for example. This takes up more space than a List or Tuple but it can allow for more efficient searching if you know the key which can be used to access the values of the specific item.

Implementation

Implementing a dictionary is a bit more complicated than creating a list, set, or tuple because of the key-value relationship. To this extent there are two main ways:

  • Using {} notation but when we have keys and values separated by a : such as {key:value}
  • Using the dict() function but only when we have a list of tuples that have two items in

The key thing here is making sure that we can create a dictionary using the key:value structure which can be created as follows:

Where we can see that we have been able to create a dictionary using both methods.

Datatypes

We have seen above that, like lists, tuples, and sets dictionaries can contain multiple different data types as values, but they can also take different datatypes as keys as well as long as they are not mutable meaning you cannot use a list or another dictionary as a key because they are mutable. We can see this as:

is a valid dictionary.

Accessing items

Accessing items in a dictionary is different from accessing items in a list or a tuple as now instead of using their numerical index we have keys which we can use to access values. This means that we can use [] notation as we have done before but there are two main ways:

  • Using [] notation to specify the key that we want to access the value for
  • Using get() method to specify the key we want to access the value for

The main difference between these is that the first method will raise an issue if the key is not in the dictionary while the second method will simply return None if the key is not in the dictionary. The choice of method of course will depend on what you want to do with the result but each of these can be implemented as follows:

Accessing information this way means that we can’t have duplicates in the keys because otherwise, we wouldn’t know what we are accessing. While creating a duplicate key when initialising the dictionary won’t create an error message, it will affect how you can access information. For example:

As we can here we set two "Name" keys and when trying to access the information it only prints the second value, not the first. This is because the second key overwrites the first key value.

Mutable

As with lists and sets, and unlike tuples, dictionaries are mutable meaning that we can change, add or remove items once the dictionary has been created. We can do this in a similar way in which we access individual items and use variable assignment ( = ) to change the actual value. We can also add new key:value pairs by simply specifying a new key and then assigning it a value (or even not). Finally, we can add a new dictionary to an existing one by using the update() method. These can all be shown as:

Thus we can change information contained in a dictionary, although we can’t change the actual key apart from deleting it. Thus, we can see how we delete items from a dictionary.

To remove items from a dictionary we can use the del method although we have to be careful with this otherwise we could delete the whole dictionary! We can also use the pop() method which we use to specify the key we want to remove from the dictionary. An extension of this is the popitem() method that in Python 3.7+ removes the last item from the dictionary (before this it was a random item). Finally, we can use the clear() method to remove all values from a dictionary. These can be implemented as:

Additional functionalities

Since we have a different structure to that of lists, tuples, or sets, dictionaries also have their own functionality to deal with that. Notably, you can use the keys() method to extract a list of all keys from the dictionary, you can use the values() method to extract a list of all values from the dictionary and you can use the items() method to extract a list of tuples of key:value pairs. Finally, you can use the len() function to extract the length of the dictionary as well. We can see this as:

This allows you the ability to check whether an item is in the keys, in the values, or in both, while also checking the list of a dictionary.

Thus, a complete-ish guide to a dictionary. The unique nature of dictionaries as having a key:value structure increases the size of storage of dictionaries but it can make retrieval more accurate if you know the key that the value is associated with, making it a valuable structure for storing layered information or information across data sources. In this way, they lay the foundation for more complex data storage methods such as Pandas DataFrames, JSON, and others.

This is the fourth article in a series exploring Data Structures and their use and implementation in Python. If you missed the first three on Lists, Tuples, and Sets you can find these at the following link:

Future articles in the series will cover linked lists, stacks, queues, and graphs in Python. To make sure you don’t miss any in the future then sign up to receive email notifications when they are published:

philip-wilkinson.medium.com

If you liked what you read and are not yet a Medium Member consider supporting both myself and other amazing authors on this platform by signing up using my referral code below:

philip-wilkinson.medium.com