About this article

In this article, I’ll explore the dictionary, one of 4 built-in types used to store collections in Python. 

This post is the fourth article in a series of four exploring the topic of built-in collection types in Python. I based the series on my notes while studying for a Python technical interview.

For quick access, here is the list of posts on this series:

  1. Python Lists
  2. Python Tuples
  3. Python Sets
  4. Python Dictionaries (this article)

Introduction to dictionaries

A dictionary is a Python data type used to store data in key: value pairs. The main characteristics of dictionaries are:

Dictionaries are ordered

Starting with Python 3.7, items in a dictionary retain the order of insertion. You can’t, however, access a dictionary element by index.

Dictionaries are mutable

You can change, add, or remove items after the dictionary creation.

Dictionary keys must be immutable

The keys of dictionary elements must be immutable. Therefore, allowed data types for keys are: booleans, integers, floats, tuples, strings, and frozensets.

Dictionary keys must be unique

A dictionary can’t have two items with the same key.

Creating a dictionary

You can create a dictionary using a literal with a sequence of key: value pair enclosed in curly braces:

You can also use the dict() constructor, passing it as an argument a sequence of key: value pairs:

You can also create an empty dictionary:

Accessing dictionary values

You access a value from a dictionary by passing the value’s key in square brackets:

You can add new keys to an existing dictionary by assigning them using the square bracket notation:

If the key already exists in the dictionary, the new one replaces the old one:

Dictionary built-in methods

dict_1.clear()

This method removes all items from dict_1. This method has a constant time complexity of O(1).

dict_1.copy()

This method returns a copy of dict_1. This method has a linear time complexity of O(n), where n is the length of the dictionary.

dict.fromkeys(keys, value)

This method creates a dictionary with the keys specified in the keys argument (a sequence). All keys get assigned the value argument. This method has a linear time complexity O(n), where n is the length of the sequence values.

dict_1.get(key, default=None)

This method returns the value for the specified key or the specified default if the key is not present. This method has a constant time complexity of O(1).

dict_1.items()

This method returns a list of tuples with the (key, value) of the items in dict_1. This method has a linear time complexity O(n), where n is the length of dict_1.

dict_1.keys()

This method returns a list of the keys in dict_1. This method has a linear time complexity O(n), where n is the length of dict_1.

dict_1.pop(key)

This method returns the item’s value with the specified key and removes the item from dict_1. This method has a constant time complexity O(1).

dict_1.popitem()

This method returns the (key, value) tuple corresponding to the last inserted item and removes the item from dict_1. This method has a constant time complexity O(1).

dict_1.setdefault(key, value)

This method returns the value from dict_1 corresponding to the passed key. If the key is not in dict_1, it adds it with the given value and returns that value. This method has a constant time complexity of O(1).

dict_1.update()

Updates dict_1 with the specified {key: value} pairs. This method has a constant time complexity of O(1).

dict_1.values()

This method returns a list of the values in dict_1. This method has a linear time complexity O(n), where n is the length of dict_1.

Conclusion

This article covered Python dictionaries, a built-in data type that stores a sequence of key: value pairs. We reviewed how to create dictionaries as well as how to access and manipulate data in them. We also covered the dictionary built-in methods and their time complexity.

You’ll likely use dictionaries in any non-trivial Python project. Hopefully, with the information covered in this article, you’ll feel confident using them.

This was the last article in this miniseries. If you’d like to keep reading, I suggest you continue with the stack, the first article in a miniseries exploring the implementation of linear data structures in Python.

References

W3Schools – Python Dictionaries

W3Schools – Python Dictionaries Methods

Python Documentation – Dictionaries

Python Time Complexity

Real Python – Dictionaries in Python