4 Dictionaries and sets

 

Dictionaries (https://docs.python.org/3/library/stdtypes.html#mapping-types-dict) are one of Python’s most powerful and important data structures. You may recognize them from other programming languages, in which they can be known as "hashes," "associative arrays," "hash maps," or "hash tables."

In a dictionary, we don’t enter individual elements, as in a list or tuple. Rather, we enter pairs of data, with the first item known as the "key," and the second item known as the "value."

This seemingly small difference, that we can use arbitrary keys to locate our values, rather than using integer indexes starting at 0, is actually crucial. Many programming tasks involve name-value pairs — such as usernames/user IDs, IP addresses/hostnames, e-mail addresses/encrypted passwords. Moreover, much of the Python language itself is implemented using dicts. So knowing how dicts work, and how to use them better yourself, will give you insights into the actual implementation of Python.

There are three main ways that I use dictionaries:

4.1  Hashing and dictionaries

4.2  Sets

4.3  How many different numbers?

4.3.1  Solution

4.3.2 Discussion

4.3.3  Beyond the exercise

4.4  Flip a dictionary

4.4.1  Solution

4.4.2  Discussion

4.4.3  Beyond the exercise

4.5  Rainfall

4.5.1  Solution

4.5.2  Discussion

4.5.3  Beyond the exercise

4.6  Dictdiff

4.6.1  Solution

4.6.2  Discussion

4.6.3  Beyond the exercise

4.7  Summary

sitemap