Hello and welcome to this tutorial on python dictionary.
A dictionary consists of a collection of key value pets and each key value pair.
Maps key toward associated revenue.
So here I have defined keys such as name age class height and weight.
And these are the values the keys and the values are separated by coolant.
So age is 5.
Class is Grade 1.
Height is 3 feet and weight is 20 kilos.
And a dictionary can be defined by enclosing comma separated key while you pairs in curly braces.
So here we have a dictionary dict to 1 which maps the keys to the values you are given key can appear
in a dictionary only once so duplicate keys are not allowed in a dictionary and an empty dictionary
is represented with a pair of curly braces.
Make this next.
Dictionaries are mutable and are un ordered collections of data.
You can add items or delete items from a dictionary without creating new copies so a dictionary can
be changed in place.
We have talked about lives before.
Lists are an ordered collection of data.
That means they retain the order of the items in which we insert it.
If we want to access or retrieve the elements in the list they are accessed using index numbers coming
to a dictionary.
A dictionary is in an ordered collection of data.
That means when you add or delete entries from a dictionary you won't be guaranteed that any sort of
order will be maintained.
And if you want to retrieve all value from a dictionary we can retrieve the value using Dickey.
Say for example a one that is aisle one 2 and 3.
I'll forward Dickies in the dictionary and if we want to retrieve any of these values we can retrieve
using the keys.
So the mapping of the values with Dickies remains fixed but not deep position.
It might vary.
So when we want to access elements in a dictionary we do not access elements based on the position we
do it based on these guys.
Next a dictionary key must be of the type immutable that means we can use an integer offload a string
or a boolean as a dictionary key coming to the dictionary value it can be of any type.
So it could include lists and other dictionaries as well and dictionaries like lists support nesting
to any debt addiction any key in appear in a dictionary.
Only once and a dictionary can be created with the burden dict function.
So this is the python built in function and to this function we need to parse arguments which are a
sequence of key value pairs say for example in these key value pairs have to be included in a list.
Say for example I have the key value PID H.
Tom next I have class as the key Grade 1 is devalued.
This is another key value pair.
And this is parsed in a list to the dict function.
So let's check this in the Jupiter notebook.
So here define a dictionary dict one with the key value pairs separated by comma and then finally enclosed
in curly places.
So this is how we can define a dictionary.
Select let's display this dictionary dict.
1.
Let us also see how we can define a dictionary using the dict function.
So we will define and not the dictionary dict to using this dict.
Function so right now since we have not included any key value pairs this is going to be an empty dictionary.
Now we will add a few key value pairs to the dictionary to do so.
Here I have created the dictionary dict to using the dict function and I have passed a list of key value
pairs as argument to the dict function so let's execute this and display the dictionary to do so.
Here is the dictionary to do displayed a value is retrieved from a dictionary by specifying its corresponding
key in square brackets.
So we have a dictionary dict to one year and say for example we want to know the items that are stored
in a one I won.
So in order to retrieve the value in the key a one we need to parse the key in square brackets like
this and then execute.
So you have your value retrieve for the corresponding key so similarly you can retrieve other keys using
the square brackets.
Say for example you want to add a key value pair to an existing dictionary.
Say for example this is the dictionary dict 1 and you want to add another aisle to the existing dictionary
so you have a 5 and you want to assign the value data in a 5 so let's now display the dictionary dict
1.
So here we have the dictionary dict 1.
The dictionary Key needs to be an immutable later type.
Dictionary value can be of any type.
So let us now create another dictionary which contained a list of elements as devalue so he could have
defined another dictionary dict 3 which has only one key country and then the value is a list of elements.
India China and Brazil.
In order to access the value in this dictionary we need to give the dictionary dict 3 and in square
brackets pass the key and the key is country.
And for example if you want to retrieve any of these elements in the list we need to add an additional
index to this or dict 3.
The first index contains the key country and within this key we want to the dream.
The second element that is China.
So the index for this element in this particular list is 1.
So this is how you retrieve the elements in a sublease.
Another example that a dictionary has another dictionary defined in it.
So here we have the dictionary dict Ford which has one key type and the value is another dictionary
which is enclosed in curly braces here.
So in order to access the value which is assigned to type which is key parse the key in these square
brackets and here you have the dictionary.
Now in order to access these sub dictionary add an additional index and here you parse parse the key
immutable which is defined in the sub dictionary.
Now when you execute this you have the value that is assigned to the key immutable.