- [Instructor] Dictionaries are fundamental data type
in the Python programming language.
These are organized in memory
as an audit, indexed and mutable data structures.
A data structure is an implementation
that a programming language provides a programmer
to store and access data efficiently.
A dictionary that we use in our daily lives
holds a word and corresponding meaning to the word.
Similarly, here a dictionary stores data in pairs.
Dictionary store a mapping of unique keys to values.
A dictionary consists of a collection of key value pairs.
This ability of storing bivariate values in a dictionary
is an important functionality in Python.
What do I mean by bivariate?
Bivariate implies the existence of two variable
where one variable influences the other.
In case of dictionaries, the key is the independent variable
while value is the dependent variable.
But it's kind of like lists.
Dictionaries are much more efficient however,
this makes dictionaries in Python
an excellent tool for conducting exploratory data analysis
while analyzing datasets.
Each key value pair maps the keys to its associate value.
You can define a dictionary by enclosing
comma separated list of key value pairs in curly braces.
A colon separates the key from its associated value.
Here is a brief example of using a dictionary
at the Python interpreter prompt.
Add the command line prompt,
type the word Python.
In the following interactive session,
the three greater than signs represent
the Python interpreter prompts.
The sal info is the name of the dictionary I'm using.
Sal info is a dictionary that holds the names
of all the major cities in the USA
and the average salaries of the
software developers as the R values.
So let's say I do, sal underscore info,
equal braces, single closed Austin,
colon 91185
comma, that separates one key from the other,
close braces, Enter.
Now sal info displays the contents of the dictionary
and this lists the list of cities
and the respective average salaries
of their software developers.
To just access the salary of Boston, say,
we can just call the key within square brackets.
So sal underscore info, Boston equal,
and we can reset the value to 95123.
So now when we do sal info,
we notice that Boston has the new value assigned to it.
Let's say we want to add a new element.
So we say sal info and add a new key of a new city,
say Atlanta, and we assign a new value to it 91234.
Now, when we do sal info, again,
it prints the contents of the dictionary including Atlanta.
Notice that the keys and the values
are displayed in the order they were inserted.
They are not sorted.
If you remember from our previous video,
the key is displayed in the order it was inserted.
In this case, Atlanta shows up as the last one.
Let's say I want to find the length of my dictionary,
I do sal info, it shows up as three.
If I want to delete one element,
I do del, name of dictionary, sal info,
square brackets, Atlanta.
Now when I see the contents again,
I notice that Atlanta no longer exists.
If I do len of Sal info,
I see that the length has been reduced by one.
Let's say I try to access an element
that's not in the dictionary.
So say I do sal info square brackets Atlanta.
It gives me a key error.
This is Python dictionary's way of giving an error
saying that particular element
does not exist in the dictionary.
Finally, let's say you want to flush out the dictionary.
That is clean it out and make it empty.
Python gives you that ability to do that
using the clear method.
So, sal underscore info dot clear.
Now, when I look at the contents by doing sal info,
I see it's an empty dictionary.
If I do length of sal info,
I see that the length is zero.
Notice the syntax.
Calling a method such as clear with parentheses
differs from calling a function
such as length with parentheses.
In clear, it's a method and a dictionary name
such as sal info and the method
are connected using the dot operator,
where the dictionary name comes first.
In a function on the other hand,
len calls the dictionary in parentheses as a parameter.
Just like in lists,
dictionaries have both functions and methods
making this data structure highly versatile.
(upbeat music)