You've heard about numbers, strings, booleans, tuples, lists, and sets, and now there's yet another data type to learn about. So, what is a dictionary? Is it a book with words in it:
Yes, that's right! And yes, that's a bad joke! But apart from that, it's also a great metaphor. The name for this Python data type was chosen on purpose.
A Python dictionary (dict) is a mapping of key-value pairs:
Keys allow you to access their associated values.
As you might see, this is equivalent to how you can use a word in a real-world dictionary to access that word's definition.
Another metaphor you can consider is comparing keys and values in dictionaries to variables and values in programming. The dictionary key represents a reference to the associated value in the dictionary.
Creating a Dictionary
You can create a dictionary using curly braces ({}) and adding keys and values that are separated by a colon (:). Multiple entries are separated by commas (,), just like elements in collections:
my_dict = {"greeting": "hello", "name": "martin"}
The dictionary my_dict has two items in it:
"greeting": "hello""name": "martin"
Each of these two items consists of a key and a value. If you take apart the first element, then you'll see that it consists of:
- key:
"greeting" - value:
"hello"
Dictionary Practice
- What are the key and value of the second item in your dictionary?
In this example, you're using strings as both the keys and values of your dictionary. But that's not a requirement. You can use any Python data type as values in your dictionary; however, you can only use immutable data types as keys:
- Keys need to be immutable data types, such as
str,int, ortuple, and need to be unique - Values can be any Python data type and can contain duplicates
Dictionaries differ from the collections you've encountered before in that you access their values not by index but with a unique key. This also means that you can't have duplicate keys in a Python dictionary. There can, however, be many duplicate values in every dictionary.
Dictionaries are extremely useful data structures that allow you to build complex programs and interactions. Now that you've created a new dictionary, how do you access a value from it?
Performing a Dictionary Lookup
Accessing a value in a dictionary given a key is called a dictionary lookup. In the following example dictionary users, that represents some fictional users and their age, you'll access the value of the key "mary" using a dictionary lookup:
users = {"mary": 22, "caroline": 99, "harry": 24}
print(users["mary"])
# OUTPUT: 22
You can access any of the other integer values with their respective keys.
Editing a Value
You can use syntax similar to a dictionary lookup to also change a value in the dictionary:
users = {'mary': 22, 'caroline': 99, 'harry': 24}
users['harry'] = 20
print(users['harry'])
# OUTPUT: 20
Because you're able to change a dictionary in place, this means that dictionaries are mutable objects, just like lists. This allows you to use dictionaries in your programs to keep track of the state and change it throughout the lifetime of your program.
However, it also means that you can run into the same issues with aliasing as you did when using lists. Return to the lesson on mutability in lists to refresh your memory on what that means and why it's important to keep track of.
Adding A Key-Value Pair
You can again use a similar syntax to add a new key-value pair to the dictionary:
users = {'mary': 22, 'caroline': 99, 'harry': 24}
users['ludvik'] = 9
print(users['ludvik'])
# OUTPUT: 9
print(users)
# OUTPUT: {'mary': 22, 'caroline': 99, 'harry': 24, 'ludvik': 9}
If you're using the syntax to change the value of a key that doesn't exist yet, it gets added as a new entry to your dictionary.
In a future lesson, you'll learn more about dictionaries as iterables and how you can iterate over their keys, values, or both.
Summary: What is a Python Dictionary
-
Dictionaries (
dict) in Python are mutable mappings that consist of key-value pairs- Keys need to be immutable data types, such as
str,int, ortuple, and need to be unique - Values can be any Python data type and can contain duplicates
- Keys need to be immutable data types, such as
-
Dictionaries are a commonly used data structure that gives you a lot of flexibility to store data and quickly find that data.