Dictionaries in Python
Coding with Python
🕑 This lesson will take about 15 minutes
A dictionary is a Python data structure that represents data as a collection of key-value pairs. This means that you can store and retrieve data values using unique keys. Each data value in the dictionary is given a unique key (a name) which makes it easy to retrieve or modify values just by referring to their key name (unlike lists, where data values are retrieved using their index). Dictionaries are also known as associative arrays, maps, or hash maps in other programming languages.
Some important things to know about dictionaries include:
Key-Value Pairs: A dictionary consists of key-value pairs. Each key is mapped to a specific value. The key and value are separated by a colon (:), and different key-value pairs are separated by commas. The collection of key-value pairs are contained between curly braces { } .
Dictionaries are mutable: This means you can modify, add, or remove key-value pairs after the dictionary has been created.
Keys must be unique: There can be no duplicates of keys in a dictionary. If you attempt to add a key that already exists in the dictionary, it will overwrite the existing value associated with that key.
Dictionaries support mixed data types: Values in a dictionary don’t all have to be of the same data type. Each value in a dictionary can be of a different data type. For example, a dictionary could contain a mix of integers, floats, strings, and boolean values (remember, string values are always contained between quotation marks. Dictionaries can even store lists or other dictionaries.
Accessing values: You can access values in a dictionary by referring to their corresponding keys using square brackets [] .
Let’s look at an example of a dictionary and how values can be retrieved and modified in Python code.
If you create a dictionary which contains several key-value pairs, the code may be made more readable by giving each key-value pair its own line, for example:
Dictionary methods
Here are some examples of some methods that can be used on dictionaries.
Dictionaries containing lists and dictionaries
Dictionaries can also contain lists or dictionaries (also known as nested dictionaries) inside them. Here are a couple of examples of how to store a list or dictionary inside a dictionary, and how to access their values.
List of dictionaries
In the previous examples, we created a dictionary containing information about one person. Let’s say you need to store organised information about several people (eg. a list of users). Well, you can create a list of dictionaries. In the list, each element is a dictionary. The dictionaries in the list are each separated by commas, and each dictionary is contained between curly braces { } . To access a value, you first need to reference the index of the dictionary in the list inside square brackets, followed by the key's name inside square brackets. Here’s an example:
Processing dictionaries and lists
There are times where you may need to process several values in a dictionary or list at once, perform advanced search or sorting operations (eg. sort a list of dictionaries containing user details by the users’ surnames), read user input into a list or dictionary, or input from a file into a list or dictionary. We will be looking at these types of tasks later in the course (particularly when we look at loops).
Next lesson: Conditional programming (if statements)