Welcome back.
In this video we're going to learn about dictionaries and dictionaries stores data in key value format
perfect for structured data like user info settings or configurations.
Think of it like a real dictionary.
You look up a word key to find its meaning, which is the value.
Dictionary is a collection which is ordered, changeable and do not allow duplicates.
Dictionaries are written with curly brackets.
The and have keys and values.
So here, in order to create the dictionary, I specified a new variable called person.
This is the dictionary.
It's of type dictionary equals sign the curly brackets.
And here we need to specify the key value pairs separated with commas.
So the first key value pair is name Alice.
The name is key.
The value is Alice.
And the key value pairs are separated with colon okay.
The second key value pair is h and the value is 30.
The third key is job and the value is engineer.
Dictionaries are written with curly brackets and have keys and values.
Dictionary items are ordered, changeable and do not allow duplicates.
Dictionary items are presented in key value pairs and can be referred to by using the key name.
So in order to access, for example, the age the value of age, we need to specify the dictionary name
followed inside the angled bracket.
The key age run the application.
We get Alice, the value of the name, the age 30 and the job engineer.
Those are the values for those corresponding and respective keys.
Dictionaries can't have two items with the same key.
So here I can't create age like this and making it 40 for example.
Okay, it will give me an error.
Let me run.
And here the duplicate values will overwrite existing values.
So here we have an error because the age is 30 and age is 40.
This will override this age.
Okay.
So this is a big problem when doing large scale applications.
You can add, modify or remove items from dictionaries.
For example I need to modify the job title.
So similar to the list, I can access the job key from the person's dictionary and specify the new value
of it, which is developer.
Now we can add a new key value pair, which is the city to the person dictionary, which is Boston.
Let me run again and here we go.
Name Alice, age 40, job developer and city Boston.
Also we can remove the key.
So here I am using delete person age.
I am deleting the age key value pair.
If we print it again name job and city.
We don't have age.
Okay, so this is how we control and, um, add remove items and key value pairs from dictionaries.
Also, there is a very important note here how to loop through a dictionary for key value in person
dot items you are accessing person which is the dictionary.
Using the items function, go and create key and value.
Let me run and here we go.
Key name.
Value Alice.
Key job value.
Developer.
Key city value.
Boston okay, this is the updated version of, uh, person dictionary okay.
Because we deleted the age.
Did you remember.
So here we have those three keys and values okay.
So this is how we loop through a dictionary.
This is how we remove a key.
This is how we add and modify the value of items.
And this is how we create dictionaries and access the values using the keys.