-
[MUSIC PLAYING]
-
-
INSTRUCTOR: Hello, everyone.
-
Welcome to another video on
Python series of tutorials
-
by Simplilearn.
-
In this session, we will look at
lists, tuples, and dictionaries
-
in Python.
-
Now let us see the
agenda for today.
-
First, we will learn
about the lists
-
in Python, its
characteristics, and explore it
-
in Jupyter Notebook.
-
Then we will learn about tuples
in Python, its characteristics,
-
and we will also explore
tuples in Jupyter Notebook.
-
After that, we will have
a look at dictionaries
-
in Python, its
characteristics, and will
-
head to Jupyter Notebook to
understand the dictionaries.
-
So let's start with
lists in Python.
-
Imagine you have
different values.
-
You have numbers,
you have strings.
-
Now if you want to
group them together,
-
you can do this job in Python
with the help of lists,
-
or you have worked with arrays
in any other language like C++
-
or Java.
-
This is the similar stuff
in here, which is list.
-
A list can be defined as a
collection of objects, values,
-
or items of different
types, and these collections
-
are enclosed within
the square brackets
-
and are separated by commas.
-
Let's understand this with
the help of an example.
-
We will create a list
and name it as list1,
-
and insert the elements
in square brackets
-
and separate them with
the help of commas.
-
List1 is equal to
square brackets 1
-
comma Adam comma 107 USA.
-
And in lists, we have our
first element at zeroth index
-
and we have 1 at 0
index, Adam at one index,
-
107 at second index,
USA at third index.
-
And talking about
the reverse index,
-
it starts with
minus 1 index value,
-
we have USA at minus 1
index value, 107 at minus 2
-
index value, Adam at
minus 3 index value,
-
and 1 at minus 4 index value.
-
We will explore the list
with various examples
-
in Jupyter Notebook in a while.
-
Till then, let's look at the
characteristics of lists.
-
First, the lists are ordered.
-
When we say that
lists are ordered,
-
it means that the items
have a defined order
-
and that order will not change.
-
If you add new items to
a list, the new items
-
will be placed at
the end of the list.
-
Second, elements of the list
can be accessed by index values.
-
Third, lists can store
various types of elements,
-
for example numbers,
strings, or lists itself.
-
Fourth, lists are mutable.
-
The list is changeable, meaning
that we can change, add,
-
and remove items in a list
after it has been created.
-
Lists allow duplicate elements.
-
Since lists are
indexed, list can
-
have items with the same value.
-
Let's have a look at some
methods of lists in Python.
-
-
The first one is append.
-
It adds an element at
the end of the list.
-
For syntax, we can
write list1.append.
-
And in round brackets, we have
to write the element which
-
we want to append.
-
The next is insert.
-
It adds an element at
the specified position.
-
Its syntax is name of
the list dot insert.
-
And in round brackets,
we will write the index
-
at which we want to insert
the element and comma,
-
and after that the element,
which we want to insert.
-
Third is extend.
-
It adds the element of a list
to the end of the current list.
-
Its syntax is list1.extend.
-
And in round brackets, we will
add the element like list2 which
-
we want to extend in the list.
-
The next is index.
-
It returns the index of the
first element with the specified
-
value.
-
And its syntax is
list1.index and the element,
-
which we want to fetch
the index eigtht.
-
The next is remove.
-
It removes the item with
the specified value.
-
Its syntax is list1.remove.
-
And in round brackets,
we write the element,
-
which we want to
remove from the list.
-
The next is sort.
-
This method is used
to the sort the list.
-
Its syntax is list1.sort.
-
The next is reverse.
-
It reverses the
order of the list.
-
Its syntax is list1.reverse.
-
Let's move to Jupyter Notebook
to explore these methods.
-
Let's start with
creating a list.
-
We'll name the list as list1,
and we'll create equal to.
-
And we'll start with
square brackets.
-
And we'll write our
first element that
-
would be apple comma space.
-
Next element,
orange comma space.
-
The next element,
banana comma space.
-
The next element, kiwi.
-
-
Now we will get
this list printed.
-
For that we, will
write the syntax print.
-
And in round brackets,
we will write
-
the list name that would be
list1 and run the command.
-
You can see here that
the list is printed.
-
Now let's create
a different list
-
with some different elements.
-
List2 equal to.
-
And in square brackets,
we will write apple.
-
Now we will insert
a list in a list.
-
For that, we will write a list
in square brackets 8 comma 4
-
comma 6 and comma.
-
The next element that
we will write orange.
-
-
The next element banana.
-
-
Now we'll print this list.
-
To print this, we'll write print
round brackets we'll write list2
-
And run the command.
-
Here we will get
the list2 printed.
-
We can access any
element in the list.
-
So for that, we will
write the syntax print.
-
We want to access the
second element that
-
will be the third
element from the list1.
-
For that, we will write
the syntax print list1.
-
And in square brackets,
we will write the index 2.
-
And run the command.
-
We will get the third element
from the list1 that is banana.
-
Now we will access an
element from the list2.
-
For that, we will write
the command print list2.
-
And in square bracket,
we will write 1.
-
And in square bracket, we will
write 2 and run the command.
-
What we get a 6.
-
So to access an element
from a list that
-
is already inserted in a list,
we have to write this syntax.
-
So this syntax tells
us that we will first
-
access the first index element
that is 8, 4, 6-- the list.
-
And in that, we will access
the second index value
-
that would be 0, 1, and 2.
-
That is 6.
-
So this is the syntax to access
an element in a list that
-
is already inserted in a list.
-
Now we will see a
reverse index order.
-
For that, we will write
the command print list1.
-
And in square brackets, we
will write the index minus 1.
-
And we will run this command.
-
And we get kiwi as output.
-
Now we will see
slicing in Python.
-
We can access a range
of items in a list
-
by using the slicing operator.
-
Now we will see an example
to see the slicing operator
-
work in Python.
-
For that, we will write
the syntax print list1.
-
And in square brackets, we'll
write 1 slicing operator 3
-
and run the command.
-
What we see is orange and banana
as an output from the list1.
-
When you write 1 ratio 3, that
is one slicing operator 3,
-
it gives you the elements
at index 1, 2, and not 3.
-
So we get the output
as orange comma banana.
-
Now we will see another example
for the slicing operator.
-
We'll write print list 1.
-
And in square brackets, we'll
write 3 and slicing operator
-
and run the command.
-
Here we can get only kiwi.
-
When we write a single number
before slicing operator,
-
it would print the
value at that index
-
value and all the other elements
that are present in the list.
-
But in this list, we have
only kiwi as the last element,
-
so it would print third index
value and not more than that.
-
Now we'll see append function.
-
For that, we'll write
the syntax list1.append.
-
And in round brackets
we will write
-
the element, which we want to
insert at the last of the list.
-
We will write guava
and run it, and now
-
we will print the list
to see the result.
-
Here we can see that guava is
added at the last of the list.
-
Now we'll see extend function.
-
For that, we'll write
the index list1.extend.
-
And in round brackets,
we will write
-
in square brackets,
the particular elements
-
we want to insert in the list.
-
So we will write watermelon
and the next element
-
we want to insert
would be muskmelon.
-
-
That would be in
inverted commas.
-
-
And we also want this
list to be printed.
-
So we'll write print list1
and execute this command.
-
Here we can get watermelon
and muskmelon added just
-
after the list.
-
Just add the last
elements of the list.
-
Now if you want to delete
a particular element from
-
the list, you can delete it
with the syntax D=E=l space,
-
the list name you want
to delete the element.
-
And in square brackets,
you will write the index
-
for the element
you want to delete.
-
We have written the
fifth index value.
-
So we'll run this command and
we'll get the list printed.
-
-
We can see that the fifth index
value, that is watermelon,
-
is not there in list1.
-
It's been deleted.
-
Now you can also do this with
another method that is remove.
-
For that, we will
write list1.remove
-
And in round brackets, we will
write the particular element
-
we want to remove.
-
We will write
that, that is kiwi.
-
And we also want the
list to be printed.
-
So we'll write print in
round brackets list1.
-
When we run this command,
we get that the kiwi is
-
being removed from the list1.
-
Now there is another
method that is pop.
-
We will write that
command print.
-
In round brackets, we
will write list1.pop.
-
-
And we'll write the
index value, what we
-
want to remove from the list.
-
That is 1.
-
And we also want the
list to be printed.
-
So for that, we'll write
the command print list1.
-
And we'll run this command.
-
-
We have not written the
name of the list correctly.
-
So now list1.
-
Now so at the one
index that was orange
-
that has been removed
from the list.
-
And now this is the new list.
-
-
Now we can see another
method that is clear,
-
that removes all the
elements from the list.
-
So this, we would apply on
the list2, list2.clear method.
-
-
And we'll also write
another command
-
that would be print list2
and execute this command.
-
We can see that the
list2 is empty now.
-
All the elements from the
list2 have been removed.
-
Now we will see another
method that is reverse.
-
For that, we will write the
command list1.reverse and round
-
brackets.
-
And we also want the
list1 to be printed.
-
For that, we will write
the command print list1
-
and execute this command.
-
Here we can see that the
list1 is being reversed.
-
Muskmelon, guava, banana, apple.
-
Now, if you want a particular
index element to be accessed,
-
you can have the syntax.
-
For that, you can write
that print list1.index.
-
-
And in round brackets,
you just need
-
to write the element for what
you want to get the index
-
and just run this command.
-
You will get the index of
the element guava that is 1.
-
We have another method
in list that is count.
-
So count is used to
give the number of times
-
the particular element
is present in the list.
-
So for that, we will
write the syntax print--
-
sorry, print.
-
And in round
brackets, we'll write
-
the list name, list1.count.
-
In round brackets, we'll
write the particular element
-
for which we want to get
the number of times that
-
has been present in the list.
-
We'll write muskmelon
and run this command.
-
It would give us the
value 1, because it's
-
been present in the list
for only a single time.
-
Now let's move to the tuples.
-
So tuple.
-
Tuple is almost same as list.
-
We can have different
types of values in tuples.
-
The difference in list
is we can change value
-
because list is mutable
and tuple is immutable.
-
That means you cannot
change the value.
-
A tuple can be defined as a
collection of objects, values,
-
or items of different
types, and these collections
-
are enclosed within
the circle brackets
-
and separated by commas.
-
Let's understand this with
the help of an example.
-
We will create a tuple
and name it as tup1.
-
And insert the elements
in round brackets
-
and separate them with
the help of commas.
-
So tup1 equal to round brackets.
-
The first element is 1 comma
and the second element is Adam,
-
third element is 107, and
the fourth element is USA.
-
Now in tuples, we have our
first element at zeroth index,
-
and we have 1 at zero
index and Adam at one index
-
and 107 at second index
and USA at third index.
-
And for the reverse index, it
starts with minus 1 index value,
-
we have USA at minus 1 value,
107 at minus 2 index value,
-
and Adam at minus 3 index value
and 1 at minus 4 index value.
-
We will explore the tuples
with various examples
-
in Jupyter Notebook.
-
But till then, let's look at
the characteristics of tuple.
-
So the first is the
tuples are ordered.
-
When we say that
tuples are ordered,
-
it means that the items
have a defined order
-
and that order will not change.
-
Elements of the tuples
can be accessed by index.
-
Tuple items are indexed.
-
The first item has index
0, the second index has 1.
-
Third, tuples can
store various type
-
of elements, that is, strings,
arrays, and many other elements.
-
The next is tuples
are immutable.
-
Tuples are unchangeable, meaning
that we cannot change, add,
-
or remove items after the
tuple has been created.
-
Next is tuples allow
duplicate elements.
-
Since tuples are
indexed, they can
-
have items with the same value.
-
Let's have a look at some
methods of tuples in Python.
-
First, that is index.
-
We can use the index
operator to access
-
an item in a tuple with
the index starts from 0.
-
Its index is the name
of the tuple dot index.
-
And in round brackets, we'll
write the element for which
-
we want to access the index.
-
The second is slicing.
-
We can access a range
of items in a tuple
-
by using the slicing operator.
-
For that, we'll write the syntax
tup1 and the range for which we
-
want to slice the tuple.
-
The third is concatenation.
-
Here we will add two tuples.
-
That is, we just have to
write the name of the tuples
-
to add them.
-
And we will place the plus
operator just between them.
-
The next is repetition.
-
We can have the same item
multiple times in a tuple.
-
For that, we will
just have to write
-
the syntax, the
name of the tuple,
-
and the multiplication
operator into the times we
-
want that value to be
present in the tuple.
-
The next is count.
-
It returns the number
of items a specified
-
value occurs in a tuple.
-
So for that, we have
the syntax tup1.count.
-
And in round
brackets, we will just
-
write the element
for which we want
-
to count the number of times
that specified value has
-
been occurred.
-
We also have another
method that is index.
-
It searches the tuple
for a specified value
-
and returns the position
where it was found.
-
Now let's move to
Jupyter Notebook
-
to explore these methods.
-
So let's start.
-
First, we will comment
that would be tuples.
-
And we will move
to the next column.
-
And first, we will
create a tuple.
-
For that, we'll write
the command tup1, that
-
would be the name of the tuple.
-
And in round brackets, we
will insert the elements,
-
that would be apple comma,
orange comma, banana comma,
-
kiwi.
-
-
Now we will print this tuple.
-
We will just have to write
print, and in round brackets,
-
the name of the tuple
and run the command.
-
We will get the tuple printed.
-
Now if you just want to insert
a single element in a tuple,
-
for that, we will write
the command tup2 equal to,
-
in round brackets,
you will write
-
a single element that would
be apple and run that command.
-
The tuple is being
created, but this tuple
-
is not being considered
as tuple in Python.
-
It has been
considered as string.
-
So let's check the
type of this tuple.
-
For that, we will
write print type.
-
And in round brackets, we'll
write the name of the tuple.
-
We want to check and
run this command.
-
Here we can see that
it is showing a string.
-
So a single element is being
shown as a string in Python.
-
If you want that
it should be tuple,
-
for that, you just have
to insert a single comma
-
in the tuple.
-
And now run this command, you
will get this type as tuple.
-
Now if you want to access any
element in a tuple, for that,
-
you just have to write
the command print tup1.
-
And in square brackets,
the index value that is 0.
-
And here you got the
value apple that is
-
at zeroth index value in tup1.
-
Now we can also access the
element in the reverse order.
-
For that, we will write
the command print tup1.
-
And in square brackets, we will
write the index value minus 1
-
and run the command.
-
Here we can get the value kiwi
from the reverse index order.
-
Now we also have the slicing
operator in tuples also.
-
For that, we will write
the command print tup1.
-
in square brackets,
the index values
-
where we will run this command
print tup1, index value 1,
-
slicing operator 3.
-
So it has printed
orange and banana.
-
So what does it means?
-
It prints the index
value 1 and 2 and not 3.
-
We also have a repetition
method in tuples.
-
For that, we just have to write
a simple syntax like print
-
in square brackets, the
element you want to repeat,
-
we'll write the element
as repeat only and comma
-
to just consider it as tuple.
-
And just after the
bracket, we would
-
have to write the
multiply operator
-
and the number of
times the element
-
should be repeated in the tuple.
-
Now we will run this command.
-
You can see that the tuple has
been created with the element
-
repeat, and the
number of times is 3
-
that we have written
just after the element.
-
So the tuple has been created.
-
Now we will see
the count method.
-
For that, we will write
the command print the name
-
of the tuple dot count.
-
And in round brackets,
we will write the element
-
for which we want to
know the number of times
-
it been present in the tuple.
-
And we will execute the command.
-
It gives us 0.
-
As our tup1 does not
consist guava as an element,
-
so it gives us the count as 0.
-
Now we'll see another
method that would be index.
-
For that, we'll write the
command print tup1.index.
-
-
And in round
brackets, we'll write
-
the element for which we want to
fetch the index of that element.
-
Banana.
-
And we'll run this command.
-
We will get the index
of the element banana
-
that is 2, 0, 1, 2.
-
So we get the index
of the element banana.
-
Now if we want to use the
append method in tuples,
-
we have to first create
the tuple into the list.
-
For that, we will write the
Command y equal to list.
-
And in round brackets,
we will write
-
the name of the tuple which we
want to convert in the list.
-
Now we will use
the append method
-
to the list, that is
y.append and we will insert
-
an element that would be guava.
-
-
And now we would just assign
the y list to a variable tup1.
-
-
We have now converted the y list
to the tuple with this syntax
-
that is tuple y, and we have
assigned it to the tup1 value.
-
Now we will print this tuple
and execute the command.
-
Here we can see
that we have used
-
the append method in the tuple.
-
First, we have changed the tuple
to the list by this command
-
y equal to list tup1.
-
Then we have used the append
method y.append and appended
-
the element guava.
-
Then we have changed the y list
to the tuple that is tup1 equal
-
to tuple, the name of the list.
-
And then we have
printed the tuple.
-
-
We can also add two
tuples by another method.
-
For that, we will write
another tuple that
-
will be tup3 equal to,
we will insert an element
-
that would be cherry.
-
-
And now we want it
to be added in tup1.
-
So we will write
tup1 equal to tup3.
-
And now we will print the
final tuple that would be tup1.
-
And run the command.
-
Here we can see
that we have added
-
two tuples, that is
tuple1 and tuple3,
-
which consists of cherry.
-
And here we get the
whole tuple as tuple1.
-
Now to execute loops in tuples,
we will write the command.
-
For that, we will write the
syntax for space a variable x
-
and the name of the tuple colon.
-
And then we will print the
particular element one by one.
-
And we will run this command.
-
Here we can see that
all the elements have
-
been printed from the tuple.
-
Now let's understand
dictionaries in Python.
-
In lists, you can
fetch the elements
-
with the help of index numbers.
-
But if you want to specify a
different type of index for it,
-
example, if you have
a list of eight values
-
and you want to fetch the fifth
one, you will use the index.
-
But if you have key
for each element,
-
then you can use the key
to access that element.
-
Example, phonebook.
-
If you want to fetch a
number, you will use a name
-
and you get a number.
-
And officially, if we
talk about dictionary,
-
if you want to understand
the meaning of a word,
-
you go to that page and
you look at the word
-
and understand
the meaning of it.
-
So this type of concept where
you have a key and corresponding
-
to that, you have a value.
-
We can achieve this with
the help of dictionaries.
-
Python dictionary can be defined
as a collection of objects,
-
values, or items of different
types stored in key value pair
-
format.
-
These multiple key
value pairs created
-
are enclosed within
the curly braces,
-
and each key is separated
from its value by the colon.
-
Let's understand this with
the help of an example.
-
Dict1 equal to curly braces.
-
The first key is a.
-
And corresponding to
that, we have value
-
and that is separated by colon.
-
And after the key value pair
that is separated by a comma,
-
the next is b key and that
has a corresponding value 2.
-
After that, we have
inserted a comma
-
to separate it
from other values.
-
Now we have a third key that is
C. And corresponding to that,
-
we have a value 3.
-
Let us see some characteristics
of dictionaries.
-
The dictionaries are ordered
from Python version 3.7.
-
When we say that
dictionaries are ordered,
-
it means that the items
have a defined order
-
and that order will not change.
-
Second, that is, elements of the
dictionaries cannot be accessed
-
by index.
-
Third, dictionaries can store
various types of elements.
-
Fourth, dictionaries
are mutable.
-
Dictionaries are
changeable, meaning
-
that we can change,
add, or remove items
-
after the dictionary
has been created.
-
Fifth one is
dictionaries doesn't
-
allow duplicate elements.
-
Dictionaries cannot have
two items with the same key.
-
Now let's see some
methods in dictionaries.
-
The first is clear method.
-
It removes all the elements
from the dictionary
-
and its syntax is the name
of the dictionary dot clear.
-
The second is get.
-
It returns the value
of the specified key.
-
The syntax is dict1.key.
-
And in square brackets, we
have to write the key name.
-
The third is keys.
-
It returns a list containing
the dictionary's key.
-
Its syntax is dict1.keys.
-
The next is pop.
-
It removes the element
with the specified key.
-
Its syntax is dict1.pop.
-
And in round brackets, we would
write the particular key name.
-
And the next method is pop item.
-
It removes the last inserted key
value pair from the dictionary
-
and its syntax is dict1.popitem.
-
Let's move to the
Jupyter Notebook
-
and execute these methods.
-
First, we will add a comment
that would be dictionaries.
-
And now we'll
create a dictionary.
-
For that, we'll write the
syntax name of the dictionary
-
equal to curly braces.
-
First, we'll write the key
value, then colon, and the value
-
corresponding to it.
-
We'll write apple, then
separated by comma.
-
And then the second key
value colon, and the value
-
corresponding to that
key that would be orange.
-
And it will be
separated by comma,
-
then we'll assign another key
value that would be 3 colon.
-
And the value corresponding
to that would be banana comma.
-
Next, the key value
would be 4 colon.
-
And in inverted commas, we'll
write the value corresponding
-
to it.
-
And we'll print this dictionary.
-
-
Print dict1 and
execute this command.
-
Here we can see that
the dictionary is
-
printed with one key value, we
have the corresponding value
-
apple.
-
With two key value, we have
the corresponding value orange.
-
With three key value, we have
the corresponding value banana.
-
And with four key value, we have
the corresponding value kiwi.
-
Now to access a
particular element,
-
we can't do that in
dictionary, but we
-
can access a particular
value with the help of key.
-
We can do that in dictionary.
-
For that, we would
write the syntax print,
-
the name of the dictionary.
-
And in square
brackets, we'll write
-
the key value that would be 1.
-
Run the command, we would
get the value corresponding
-
to the key value
that is apple that
-
is corresponding
to the key value 1.
-
Now we will see a get
method in dictionary.
-
For that, we'll write
the command print
-
name of the dictionary dot get.
-
And in round brackets, we'll
just write the key value
-
and run this command.
-
We'll get the corresponding
value to the key value.
-
That is apple.
-
Now we will use another
method that would be pop.
-
For that, we'll write
the command print dict1.
-
Pop.
-
And in round brackets,
we'll write the key value
-
for the value we want
the element to be
-
removed from the dictionary.
-
We'll execute this command.
-
And we get kiwi as the output.
-
Now if we print the
dictionary, print dict1,
-
we can see that the fourth
key value and the value
-
corresponding to
that key value has
-
been removed by the pop method.
-
Now we can also print the
length of the dictionary.
-
For that, we'll write
the command print len.
-
And in round brackets, we'll
write the name of the dictionary
-
and just execute the command.
-
We will get 3 as
the output as it
-
has been considered one unit,
second unit, and third unit.
-
So the length is 3.
-
And we have another
method that is sorted.
-
For that to write the command
print sorted and the name
-
of the dictionary dict1.
-
And we'll execute this command.
-
It would give us all the key
values in a sorted manner
-
when executed with this command.
-
Now we can see another
method that is keys.
-
For that, we have to write the
command print round brackets,
-
name of the dictionary
dot keys, round brackets.
-
And we'll run this
command and we'll
-
get all the keys that are
present in dictionary.
-
There is another
method that is values.
-
For that, we will write the
command print dict1.values round
-
brackets.
-
And we will execute
this command.
-
We will get all the values that
are present in the dictionary.
-
Now we will see
another method, that
-
is update that is used to
update the particular value
-
corresponding to the
key value or update
-
the dictionary with more
values to be inserted
-
in the dictionary.
-
For that, we'll write
the command dict1.update.
-
-
In round brackets, first, we
will write the curly braces,
-
the key value semicolon colon.
-
And corresponding to that,
we will write its value,
-
which we want to update.
-
Watermelon.
-
And we'll also print
the dictionary.
-
We will execute this command.
-
We will get that the second
value that is watermelon,
-
that's been updated.
-
Previously, it was orange
and now it's watermelon.
-
So it's been updated
by this command.
-
And we can also use this
command to add more elements
-
to the dictionary.
-
For that, we'll write the
syntax dict1.update round
-
brackets, curly braces,
and the key value colon.
-
And corresponding to that, we
will write the value muskmelon
-
and we'll print the dictionary
and we'll execute this command.
-
-
Sorry, we just omitted the name
of the dictionary, dict1.update.
-
Now we'll execute this command.
-
Here we can see
that the value has
-
been added to the dictionary.
-
-
Now we'll see another
method that is pop.
-
For that, we'll write
the command dict1.pop.
-
And in round brackets,
the key value
-
for which we want the value to
be removed from the dictionary.
-
And we will print the dictionary
and execute this command.
-
Here we can see that the
second key value has been
-
removed from the dictionary.
-
And we will see another method
that would be dict1.popitem.
-
-
And then we will
print the dictionary.
-
-
And execute the command.
-
Here we can see that
the pop item has removed
-
the last element
from the dictionary,
-
and we are left with only two
elements in the dictionary.
-
Now we will see some
loops in the dictionary.
-
For that, we'll
write the command
-
for x in the name of the
dictionary, that is dict1 colon
-
and then we'll
write print dict1.
-
And in square brackets,
the variable x.
-
And we will execute
this command.
-
So with this loop, we can print
all the values corresponding
-
to the key values
in the dictionary.
-
Now to print both the keys and
values corresponding to it,
-
we can run a loop.
-
For that, we'll
write the command
-
for x comma y in dict1.items
colon print x comma y.
-
And run this command.
-
We can get the key and the
values corresponding to it.
-
-
Now if you want to copy
the particular dictionary
-
in another dictionary,
you can write the command.
-
First, you will
name the dictionary
-
you want to create
and copy the elements.
-
We will name it as
mydict equal to.
-
Now we'll write the
command dict1.copy.
-
-
And then we will print the new
dictionary, that is mydict.
-
-
And we'll run this command.
-
We have not inserted
the round bracket here.
-
Now run this command.
-
Here we can see that we have
printed the mydict dictionary.
-
And it has the same
values as dict1.
-
Now we'll see another method
to delete the whole dictionary.
-
For that, we'll
write dict1.clear.
-
-
It would remove all the
values from the dictionary
-
and will also print the
dictionary after that.
-
Run this command.
-
Here we can see all the
elements from the dictionary
-
have been removed.
-
With that, we have come to
the end of this session.
-
I hope it was interesting
and informative.
-
If you liked it, please let
us in the comment section.
-
Also do subscribe to our
channel and stay tuned
-
for more from Simplilearn.
-
-
Hi there.
-
If you like this video,
subscribe to the Simplilearn
-
YouTube channel and click
here to watch similar videos.
-
To nerd up and get
certified, click here.
-