WEBVTT

00:00.120 --> 00:01.280
Welcome back.

00:01.320 --> 00:08.040
In this video we're going to learn about dictionaries and dictionaries stores data in key value format

00:08.040 --> 00:12.360
perfect for structured data like user info settings or configurations.

00:12.520 --> 00:14.400
Think of it like a real dictionary.

00:14.440 --> 00:19.040
You look up a word key to find its meaning, which is the value.

00:19.360 --> 00:25.360
Dictionary is a collection which is ordered, changeable and do not allow duplicates.

00:25.400 --> 00:29.760
Dictionaries are written with curly brackets.

00:29.920 --> 00:32.360
The and have keys and values.

00:32.640 --> 00:39.000
So here, in order to create the dictionary, I specified a new variable called person.

00:39.000 --> 00:40.080
This is the dictionary.

00:40.080 --> 00:45.360
It's of type dictionary equals sign the curly brackets.

00:45.520 --> 00:52.320
And here we need to specify the key value pairs separated with commas.

00:52.680 --> 00:56.960
So the first key value pair is name Alice.

00:57.080 --> 00:58.520
The name is key.

00:58.720 --> 01:00.160
The value is Alice.

01:00.160 --> 01:04.880
And the key value pairs are separated with colon okay.

01:05.360 --> 01:11.000
The second key value pair is h and the value is 30.

01:11.040 --> 01:15.450
The third key is job and the value is engineer.

01:15.490 --> 01:21.490
Dictionaries are written with curly brackets and have keys and values.

01:21.730 --> 01:26.690
Dictionary items are ordered, changeable and do not allow duplicates.

01:26.730 --> 01:35.050
Dictionary items are presented in key value pairs and can be referred to by using the key name.

01:35.050 --> 01:45.170
So in order to access, for example, the age the value of age, we need to specify the dictionary name

01:45.330 --> 01:49.410
followed inside the angled bracket.

01:49.410 --> 01:52.930
The key age run the application.

01:53.170 --> 02:00.490
We get Alice, the value of the name, the age 30 and the job engineer.

02:00.650 --> 02:07.290
Those are the values for those corresponding and respective keys.

02:07.330 --> 02:11.610
Dictionaries can't have two items with the same key.

02:11.810 --> 02:19.370
So here I can't create age like this and making it 40 for example.

02:19.410 --> 02:21.770
Okay, it will give me an error.

02:21.810 --> 02:22.810
Let me run.

02:23.090 --> 02:28.650
And here the duplicate values will overwrite existing values.

02:28.650 --> 02:32.530
So here we have an error because the age is 30 and age is 40.

02:32.730 --> 02:36.130
This will override this age.

02:36.490 --> 02:37.010
Okay.

02:37.210 --> 02:42.130
So this is a big problem when doing large scale applications.

02:42.290 --> 02:47.370
You can add, modify or remove items from dictionaries.

02:47.450 --> 02:52.050
For example I need to modify the job title.

02:52.050 --> 03:01.770
So similar to the list, I can access the job key from the person's dictionary and specify the new value

03:01.770 --> 03:03.490
of it, which is developer.

03:03.650 --> 03:12.010
Now we can add a new key value pair, which is the city to the person dictionary, which is Boston.

03:12.170 --> 03:14.810
Let me run again and here we go.

03:14.970 --> 03:19.970
Name Alice, age 40, job developer and city Boston.

03:20.010 --> 03:22.330
Also we can remove the key.

03:22.450 --> 03:27.050
So here I am using delete person age.

03:27.210 --> 03:31.410
I am deleting the age key value pair.

03:31.450 --> 03:35.620
If we print it again name job and city.

03:35.660 --> 03:37.380
We don't have age.

03:37.580 --> 03:47.660
Okay, so this is how we control and, um, add remove items and key value pairs from dictionaries.

03:47.700 --> 03:56.620
Also, there is a very important note here how to loop through a dictionary for key value in person

03:56.620 --> 04:00.780
dot items you are accessing person which is the dictionary.

04:00.820 --> 04:07.660
Using the items function, go and create key and value.

04:07.820 --> 04:09.780
Let me run and here we go.

04:10.140 --> 04:11.340
Key name.

04:11.380 --> 04:12.460
Value Alice.

04:12.500 --> 04:13.980
Key job value.

04:13.980 --> 04:14.900
Developer.

04:14.940 --> 04:16.900
Key city value.

04:17.140 --> 04:23.220
Boston okay, this is the updated version of, uh, person dictionary okay.

04:23.260 --> 04:25.620
Because we deleted the age.

04:25.660 --> 04:26.740
Did you remember.

04:26.740 --> 04:31.220
So here we have those three keys and values okay.

04:31.420 --> 04:34.380
So this is how we loop through a dictionary.

04:34.580 --> 04:36.300
This is how we remove a key.

04:36.580 --> 04:41.740
This is how we add and modify the value of items.

04:41.740 --> 04:46.900
And this is how we create dictionaries and access the values using the keys.
