A dictionary is a built-in data type in Python. It is structured based on JSON files.
In JSON format you start a JSON file like the following:
{}
In Python, dictionaries are declared in the same way. {} curly brackets are used to store formatted data.
Here are some examples…
# List
l = []
# Tuple
t = ()
# Set
s = set()
# Dictionary
d = {}
Each of these uses characters to initialize its data type.
Table of Contents
How Dictionaries are Formed
Dictionaries have to follow a format.
An example…
example = {"key": "value"}
Key to Value Pair
Dictionary data always exists in a key to value pair, like the above. Each key has a value and can never exist without a value.
For example this is wrong…
example = {"key"} # wrong, will error
But, this is right…
example = {} # valid
You can either have no key or a key-value pair. That is it.
A key is always a string and must be unique so that it represents a specific value.
If you pick two equal keys within one dictionary, the one that comes afterward will be chosen.
example = {
"key1": "value",
"key1": "other value"
}
print(example["key1]) #<- other value
Data types in dictionaries
Values are only allowed certain data types.
Here they are….
- Strings
- Integers’
- Floats
- Booleans
- Lists
- None
- Inner dictionaries
This means we can look at our original example to include more types.
This one…
employees = {
"John": {
"US Citizen": True,
"age": 30,
"address": {
"city": "Mexico City",
"country": "Mexico"
}
},
"Joan": {
"US Citizen": False,
"age": 21,
"address": {
"city": "Austin",
"country": "USA"
}
}
}
“John” & “Joan” are keys and they contain a value of an inner dictionary.
Those inner dictionaries contain data types which are allowed according to the list above.
Dictionaries vs lists
A dictionary is a direct mapping between keys and values.
So every key within a dictionary is called with a string.
Like so…
#Look at employees above....
print(employees["john"]) #<- the key is called
'''Output
{
"US Citizen": True,
"age": 30,
"address": {
"city": "Mexico City",
"country": "Mexico"
}
}
'''
However with a list, you must use an index number.
Take a look…
namesList = ["john", "mary", "joseph"]
print(namesList[2]) #<- the position is called
'''Output
joseph
'''
Instead of a key, we have a number denoting the position within namesList[].
How do you include inner dictionaries & lists?
Inner dictionaries and lists are created with their original characters.
Here, take a look…
d = {
"dictionary": {},
"list": []
}
A dictionary works within a dictionary and can be nested indefinitely.
A list is simply any number of values ordered after another and not a key-value pair.
Why can’t we use key-value pairs?
Because it is not a direct mapping but an order of values.
Here is an example of a valid list…
d = {
"randomList": [
"yes", 1, True, None, 1.1, {}, []
]
}
for element in d["randomList"]:
print(str(element))
'''Output
yes
1
True
None
1.1
{}
[]
'''
The different elements of the list are simply called by its position.
Keys are refreshed within each dictionary
Within each dictionaries keys are reset. Which is why we can have repeating key names within different inner dictionaries.
Let’s look at the example again…
employees = {
"John": {
"US Citizen": True,
"age": 30,
"address": {
"city": "Mexico City",
"country": "Mexico"
}
},
"Joan": {
"US Citizen": False,
"age": 21,
"address": {
"city": "Austin",
"country": "USA"
}
}
}
Notice within “John” there is an inner dictionary.
It contains “US Citizen”, “age” & “address” as key-value pairs.
Also notice “Joan” has the same keys.
Why are the keys re-used?
Because we use an inner dictionary and the dictionary outside only applies to the outside.
The inner keys only apply to the inner dictionary.
Let’s look at an example:
print(str(employees["John"]["US Citizen"]))
print(str(employees["Joan"]["US Citizen"]))
'''Output
True
False
'''
We are calling the dictionary within the dictionary, and within that inner dictionary there are new key-value pairs.
How to add to a dictionary
To add to a dictionary you can first initialize it…
dictionary = {}
Next up we define a key…
dictionary["key"] = "value"
print(dictionary)
'''Output
{
"key": "value"
}
'''
If we wanted to define a list, we can do so within a key-value pair.
For example:
dictionary = {}
dictionary["key"] = "value"
dictionary["list"] = []
print(dictionary)
'''Output
{
"key": "value",
"list": []
}
'''
And when we add to this list, it is the same as a regular list in Python.
We can use the append command to add values within the list…
dictionary = {}
dictionary["key"] = "value"
dictionary["list"] = []
dictionary["list"].append("element")
dictionary["list"].append(15)
print(dictionary)
'''Output
{
"key": "value",
"list": ["element", 15]
}
'''
Inner dictionaries are the same:
dictionary = {}
dictionary["key"] = "value"
dictionary["list"] = ["element", 15]
dictionary["dictionary"] = {}
dicionary["dictionary"]["innerKey"] = "innerValue"
print(dictionary)
'''Output
{
"key": "value",
"list": ["element", 15],
"dictionary": {
"innerKey": "innerValue"
}
}
'''
Defaultdict
If you try to call a key-value pair that does not exist, you get an error in Python.
dictionary = {}
dictionary["key"] = "value"
print(dictionary["key1"])
'''Output
Traceback (most recent call last):
File "C:\test.py", line 3, in <module>
print(dictionary["key1"])
KeyError: 'key1'
'''
The error appears because there is no key value pair with “key1” as the key.
Because of this, an alternate dictionary is often used.
It is called “defaultdict”.
Defaultdict sets values automatically by default when a key does not exist.
Take a look:
from collections import defaultdict
default_false_dict = defaultdict(bool)
default_false_dict["key1"] = True
default_false_dict["key2"] = True
print(default_false_dict["key1"]) # True
print(default_false_dict["key3"]) # False (default value)
By default all missing keys will be a boolean.
Which boolean? False.
Now let us look at another example:
from collections import defaultdict
default_false_dict = defaultdict(int)
default_false_dict["key1"] = 50
default_false_dict["key2"] = 14
print(default_false_dict["key1"]) # 50
print(default_false_dict["key3"]) # 0 (default value)
A default dictionary with a default of integer is 0.
This is highly useful.
But what if you wanted some specific value?
Well defaultdict has you covered:
from collections import defaultdict
default_false_dict = defaultdict(lambda: "default")
default_false_dict["key1"] = 50
default_false_dict["key2"] = 14
print(default_false_dict["key1"]) # 50
print(default_false_dict["key3"]) # "default"
Just use the lambda operator and whichever value you want after.
Dictionary .get(key, default_value)
However, let us say you want to use a default value but don’t want to create a defaultdict.
Let us see an example of that with the “get” function.
ordinary_dict = {"key1": 50, "key2": 65}
# Example usage with get() to create a default value
value1 = ordinary_dict.get("key1", None)
value3 = ordinary_dict.get("key3", None)
print(value1) # 50
print(value3) # None (default value)
The get function calls a key and if that key is missing uses None as a default value.
This is useful for algorithms and cases where you want different default values at different times.
Dictionary .keys()
This is a function which only works for dictionaries.
For this example let us revisit the previous example:
employees = {
"John": {
"US Citizen": True,
"age": 30,
"address": {
"city": "Mexico City",
"country": "Mexico"
}
},
"Joan": {
"US Citizen": False,
"age": 21,
"address": {
"city": "Austin",
"country": "USA"
}
}
}
Let us use employees as an example and get the keys of the dictionary:
print(employees.keys())
'''Output
dict_keys(['John', 'Joan'])
'''
The results are a custom datatype called dict_keys.
But for our purposes, think of them as a list.
We have a list of keys.
But notice the keys are only within the outer most dictionary.
When we call keys() it only shows the keys for that level of dictionary.
Inner dictionaries have to be called individually.
Take a look:
print(employees["John"].keys())
'''Output
dict_keys(['US Citizen', 'age', 'address'])
'''
Within “John” you have “US Citizen”, “age” & “address”.
That inner dictionary’s keys are called.
But look further, isn’t “address”’s value also a dictionary?
Let us look into the keys for that one:
print(employees["John"]["address"].keys())
'''Output
dict_keys(['city', 'country'])
'''
We are calling the keys within the inner, inner dictionary.
Pretty cool right?
Dictionary .values()
The same type of information and format can be seen, but as values.
This only works for dictionaries.
Take a look:
print(employees.values())
'''Output
dict_values([
{
'US Citizen': True,
'age': 30,
'address': {
'city': 'Mexico City',
'country': 'Mexico'
}
},
{
'US Citizen': False,
'age': 21,
'address': {
'city': 'Austin',
'country': 'USA'
}
}
])
'''
The actual values in the key pair of the outer most dictionary is called.
This can be applied to inner dictionaries…
print(employees["John"].values())
'''Output
dict_values([
True,
30,
{
'city': 'Mexico City',
'country': 'Mexico'
}
])
'''
At this inner level, .values() is calling a list of the value side of the key-pair.
We can go into an inner inner dictionary:
print(employees["John"]["address"].values())
'''Output
dict_values(['Mexico City', 'Mexico'])
'''
And, we go to the inner most dictionary and get all the values of “address”.
Pretty cool right?
Dictionary .items()
All of the above applies, but for .items() we get the key: pair itself…
print(employees["John"].items())
'''Output
dict_items([
('US Citizen', True),
('age', 30),
('address', {
'city': 'Mexico City',
'country': 'Mexico'
}
)
])
'''
These key pairs are tuples of size 2. They are always tuples of size 2 because a key-pair is two items.
A tuple is a list in python which cannot be changed.
So what you are returning here with items is a list of unchangeable key-pairs.
You can get the key-pair of any inner dictionary:
print(employees["John"]["address"].items())
'''Output
dict_items([
('city', 'Mexico City'),
('country', 'Mexico')
])
'''
Pretty awesome right?
You can iterate through keys(), values() & items()
The purpose of these three functions is to go through them and use them.
Take a look…
for key in employees.keys():
print(key)
for value in employees.values():
print(value)
for key, value in employees.items():
print("key: " + str(key) + " value: " + str(value))
'''Output
John
Joan
{'US Citizen': True, 'age': 30, 'address': {'city': ...
{'US Citizen': False, 'age': 21, 'address': {'city': ...
key: John value: {'US Citizen': True, 'age': 30, ...
key: Joan value: {'US Citizen': False, 'age': 21, ...
'''
You can create all kinds of logic with dictionaries.
Often there are problems which can only be efficiently solved with dictionaries.
Anywho, I hope you learned something…
Happy coding!
Resources
Want to learn about slices?: https://medium.com/gitconnected/an-easy-how-to-guide-to-slicing-lists-in-python-3-51682ab4f17b