Hi Everyone, in this video I want to talk
to you about dictionaries in Python, in particular,
we'll talk about what a dictionary in python
is, and how you can implement them into your
Python codes.
So a dictionary in Python is quite like it
sounds.
If you think about what a dictionary is, its
a word and then the meaning of that word beside
it.
Well, in python the word you look up is called
the key and the meaning or association of
that key is called a value.
Therefore, every key has a value paired to
it.
These are called key-value pairs.
So what do these even look like in Python
and how do we implement them?
Well, let's just say that you want to create
a dictionary that stores some key metrics
for a Youtube channel.
Let's name it youtube metrics and to begin
our dictionary we use curly brackets.
Then you enter each key-value pair like so,
so just notice that for string values we need
the single quotation marks, and for the integers
we do not.
So we basically have a key like name, subscribers,
and views and then the values associated with
those after the colon, and as you can see
here you separate the key-value pairs through
a comma much like we did in lists.
To add another key-value pair to a dictionary
in Python, all you need to do is put your
dictionary name, put the key in square brackets
and equals the value-pair of that key, as
you can see here when we print the dictionary,
the watch time metric was appended to the
end of the dictionary.
If you want to call any item in your dictionary,
you will be calling the key and being returned
its value-pair, so if we want to say how many
subscribers we currently have, we would do
the following, and as you can see we are clearly
returned the value-pair and not the key, which
should make sense right?
When you look up what a certain word in the
dictionary means, youre looking for the associated
value to that word and not just that specific word.
Dictionaries in Python are the same way.
To remove a key-value pair from a dictionary,
all you need to do is put del the dictionary
name and then the key to the key-value pair
that you want removed.
As you can see here when we print the list
the watch time metric is now gone.
Lastly, if you wish to alter any of the values
in your key-value pairs, all you do is the
same thing as adding a new key-value pair
except just use the same key and overwrite
the value with your new value.
Thank you for checking out this video and
I hope it helped your understanding about
what dictionaries in python are and how you
can add them to your Python code.
If you enjoyed please like and subscribe to
support the channel.
However, if you have any comments, questions,
or concerns about the information I provided
in this video leave a comment below and I
will do my best to address your concerns.
