A Professional’s Tutorial to Python Dictionaries
Dictionaries, a fundamental Python data structure
--
Welcome to third, in a series of tutorials that teach beginner Python specifically for aspiring data scientists. For an overview of these tutorials, click here.
Introduction
In the realm of Python programming, the term “dictionary” assumes a meaning that is distinct from the conventional books filled with word definitions, etymologies, and pronunciations that many of us have encountered in our academic pursuits.
While both types of dictionaries serve as repositories of information, the nature and structure of the data they hold differ.
A traditional dictionary, familiar to most as an essential reference tool, organizes words alphabetically, offering definitions, usages, and sometimes even historical word origins and etymological linguistic developments. Its primary purpose is to aid users in understanding and employing language correctly. When printed as books these dictionaries follow a linear predictable format flowing from A to Z.
Conversely, Python dictionaries, commonly termed dict
in code, are an integral data structure of the Python programming language. Instead of words and their definitions, they store pairs of keys and values.
Thinking back to the book, you would analogize the dictionary word entry as a “key” and the definition itself as the “value.”
Did you get that. Here it is again. . . the term "key" can be likened to a word in a conventional book-based printed dictionary, and "value" can be likened to that word's definition.
However, in a Python dictionary, keys can represent any immutable data type (usually a number or string). The corresponding values can be just about any type of data supported in Python, ranging from numbers to lists or even other embedded dictionaries.
Unlike their traditional bookshelf paper counterparts, Python dictionaries are not inherently ordered. Although recent versions of Python do now maintain insertion order…