Python sets and dictionaries

Python sets and dictionaries

In previous articles on this blog, we’ve discussed Python tuples and lists. Today we’ll be talking about another useful Python data type: dictionaries.

Understanding Python Dictionaries

Unlike lists and tuples, which are both sequence types, a Python dictionary is a mapping object which is also mutable. Objects which map (link) two pieces of information together are just as helpful in programming as they are in real life. For example, word dictionaries map a word to it’s meaning and part of speech. Phone number listings, whether online or in paper phone books, match a person’s name with their phone number.

In order to retrieve either a word’s definition or someone’s phone number, a person first has to first search for and locate the word or full name, respectively. In both cases, one piece of information is used to retrieve another.

Python dictionaries work the same way, storing and retrieving information in the form of key-value pairs. The “key” is an unique identifier (like a name or label) within the dictionary which also must be hashable, and therefore an immutable type (such as strings or numbers). Thus lists can’t be used as dictionary keys, but tuples can, as long as they don’t directly or indirectly contain a mutable object, something we demonstrated in our article on tuples. The values stored in a dictionary can be any data type, mutable or immutable.

Since they are mapping objects, Python dictionaries are indexed by keys, not by a range of numbers as sequences are. Keys are like static labels which can be used to easily access changing values.

Using Dictionaries in Python Programs

Being mutable, Python dictionaries are designed to have their elements updated, added to, and even deleted. This makes them great for programs which require storing and then retrieving the most current values of a given process or object. It also helps that dictionary lookups in Python are fast, due to the fact that they are implemented as hash tables, hence the requirement for the dictionary keys to be hashable.

Like lists, dictionaries can also be “popped” (i.e. used as a LIFO queue) using the popitem() method, a feature which makes it easy to iterate over a dictionary while moving its contents to another container, like a list or set. Starting with Python 3.7, the popped pairs are guaranteed to be returned in LIFO order.

Although the standard use case for dictionaries is to store and retrieve the value associated with a key, keys, values, and both can be listed using dict.keys(), dict.values(), and dict.items(), respectively. These methods create what are called view objects, and they are dynamic views of the dictionary they are generated from, which means that any change in the dictionary’s keys or items is immediately reflected in the dictionary view. Dictionary views can be iterated over and also support the same type of membership tests (e.g. union and intersection) that sets do.

Python dictionaries make it easy to access information using convenient names, instead of the index or slice notation used with lists and tuples. They are an excellent tool for fast mapping operations, and thus should be well understood by all Python developers, experts and novices alike.

Copyright © Python People