Welcome to Engineering Python.
This is a Python programming course for engineers.
In this video, I'll talk about dictionaries.
A dictionary is a list of key: value pairs enclosed by curly braces.
For example, inventory is a dictionary of three key: value pairs.
apples, 430 kg, bananas, 312 kg, oranges, 525 kg.
Apples, bananas, and oranges are the keys, 430, 312, and 525 are the values.
The type function shows it's a dict, which is short for dictionary.
If we would like to know the current inventory of bananas, we use the key bananas as the
index.
It returns 312.
The length of inventory is 3 because it has 3 key: value pairs.
The in keyword checks whether a key is in the inventory.
apples are in this inventory.
So, the output is True.
blueberries are not in this inventory.
So, the output is also True.
The keys must be unique in a dictionary.
In this example, we have two keys with the same name, apples.
The created inventory will keep just one of them.
The key: value pairs are unordered in a dictionary.
For example, we define a dictionary with three kids' information, their names and ages.
First kid, Emma is 14 years old, second kid, Tom is 10, third kid, Jacob is 8.
When we show this dictionary, Jacob and Tom switched palaces.
We can also convert a list of tuples that contain key: value pairs to a dictionary using
the dict function.
The first element in each tuple will be the keys, and the other element will be the value.
We successfully created the kids dictionary using this method.
If we want to see just the keys, we can use the keys method.
It returns the dictionary keys Emma, Tom, and Jacob, in the order the dictionary was
defined.
We can convert the keys to a list using the list function.
We can also sort the keys alphabetically using Python's built-in sorted function.
If we want to see just the values, we can use the values method.
It returns the values, 14, 10, and 8, in the order the dictionary was defined.
If we want to see both the keys and the values at the same time, we can use the items method.
It returns a list of the dictionary's key value tuples.
When looping through dictionaries, the key and the corresponding value can be unpacked
from the return of the items method.
This example prints both the names and the ages.
When the keys are simple strings, we can define the dictionary in a different way.
spanish is a dictionary defined using the dict function.
The English words hello and yes are the keys, and the spanish translations hola and si are
the values.
See the output.
hello and yes are changed to strings.
However, we should not write them as strings like this when we define this dictionary.
This is wrong.
Like lists, dictionaries are mutable.
This is different from tuples, which are immutable.
Let's use this inventory as an example.
This del statement will delete bananas from this inventory.
After deletion, inventory only has apples and oranges.
This statement will set the inventory of oranges to 0 kg.
It's changed to 0.
We can create an empty dictionary spanish with a pair of empty braces like this or with
the dict function, no parameters.
We can later add two key: value pairs into the dictionary.
Now it's not empty.
We can make it empty again by clearing all the elements in the dictionary.
Now it's empty again.
Finally, let's talk about dictionary comprehensions.
Dictionary comprehensions are similar to list comprehensions.
They can be used to create dictionaries from compact key and value expressions.
This is an example.
The outermost symbols are a pair of braces.
Inside the braces, we have a key:value pair, they are separated by the colon mark.
x is the key, x squared is the value, for every x in this tuple.
As a result, we have a dictionary of three pairs, 2, 4, and 6 are keys, and 4, 16, and
36 are the values.
This dictionary comprehension is equivalent to the following code.
Initialize the variable squares to be an empty dictionary.
Use the for loop and x as the loop index to loop through all the values in this tuple.
For each loop, add an item to the dictionary, using x as the key and x squared as the value.
The result is exactly the same as the one created using the dictionary comprehension.
Okay, that was all about dictionaries.
The course materials are available on YouTube and GitHub.
You can watch the course videos in sequence.
If you like this video, please subscribe and share.
I'm Yong Wang.
Thanks for watching.