This is a text field

Python dictionary- Value of a key!

In the previous blogs, you had seen that how the data types are working. To be confirmed You can check in my previous blogs.

In this blog, you are going to see why we using a dictionary in python?

What is a dictionary? and How to use a dictionary?

Why Use Dictionary?

If you have to access data easily, store key-value pair as one element, You can use a dictionary in python.

For example, {name:student_name}.

You can access data with curly braces({}).

What is a dictionary?

A dictionary in python is key-value pair. It is ordered, changeable and doesn’t allow duplicate items in python.

Remember, till version 3.7 python dictionary was an unordered collection of data types. Because of Insertion after this version python became ordered.

How to use a dictionary?

You can use a dictionary with the help of key-value pair. You can access any value in key with the help of key.

In this blog, you are going to see:-

•How to access values?
•How to remove the     
value?
•How to add the values  
in the dictionary?
•len() method
•looping in the         
dictionary
•clear the dictionary
•To delete the          
dictionary.
•Copy the dictionary
•Nested dictionary

Python Dictionary- Access the value

You can access the value with the help of a key inside the square bracket.

For example,

Value: 20

You can see that accessing the value with key is an easy comparison to the indexing of value.

Python- Remove the values

Suppose, you have to remove some values from your college dictionary. How will you remove it?

You can remove it with the help of pop() and popitems().

A pop() method

You can remove the specified value with the help of the pop() method.

The syntax of the pop() method is pop(key).

For example,

Here, you can see that first I have removed the key age and printed it.

After, it we had printed the original dictionary. You can see that in the original dictionary the key age had removed.

It doesn’t need indexing to remove value.

The second is the popitems() method you are going to try.

The popitem() method

Suppose, you have to remove the last value from the college dictionary. How will you remove it?

You can remove it with the help of the popitem() method. Because it removes only the last key. so it doesn’t take any argument.

For example,

Here, you can see that the last key “std” has removed from the dictionary of college.

Python- Add the values in Dictionary

You can add the values in the dictionary with the help of assigning the new value to a new key

For example,

Here, In the output, you can see that before and after assigning the value, how the dictionary was.

Python- len() method

Suppose you have to find the number of key-value pairs present in the college dictionary.

For example,

Here, you can see that 3 key-value pairs are present.

Python- Looping the dictionary!

Suppose, you want the sequence of dictionary items. So how will you do it?

You can do it with the help of for loop in dictionary.

For example,

Here, you can see that the sequence has come to the keys of the dictionary.

Suppose you want the sequence of value. How can you do it?

For example,

Python Dictionary- clear() method

Suppose, you want a college dictionary empty. But the college dictionary is full of key-value pairs. How will you do it?

You can do it with clear() method.

For example,

Here, you can see that an empty dictionary, with curly braces, had come.

Python dictionary- del keyword

Suppose, you not only clear the dictionary but also delete the whole dictionary. So, what you will do?

You can do it with the help of del keyword.

For example,

Here, you can see that NameError has come. Because we have deleted the dictionary with the help of the del keyword.

Python Dictionary- Make a copy of the dictionary

Suppose, you have to make a copy of the dictionary. how will you do it?

You can do it with the following method:-

•copy() method
•dict() method

1.copy() method

In the copy() method you copy one dictionary into another dictionary.

For example,

Dictionary: x

2. The dict() method

You can also use the dict() method to copy the dictionary.

For example,

Dictionary: x

Here, you can see that the dict() method copied the dictionary.

Python- Nested Dictionary

Why use a nested dictionary?

Nested dictionary use for storing one dictionary into another dictionary.

It stores many dictionaries within one dictionary.

How to use a nested dictionary?

You can use a nested dictionary with the help of storing dictionaries into the key of the parent dictionary.

For example,

Here, you can see that we have stored student and teacher detail in the nested dictionary.

The benefit of the nested dictionary is that you can store multiple dictionaries in one variable.

Python- Change dictionary items

Suppose, you have to change the value of a specific key in the dictionary. What you will do?

You can do it with the help of referring new value to that key.

For example, you have to change the name of the student.

Let’s see how can you do?

You can also use the update() method to change the value of the dictionary.

The update() method

The update() method update the dictionary value. Suppose you have to update the age of the student. You can do it with the update() method.

For example,

The output of assigning new value and update() method has same.

The keys() method

Suppose, you want all keys from a dictionary. So how will you do it?

You can do it with the keys() method.

The return value is a list of keys.

For example,

Here, you can see that all list of keys has come from the dictionary.

In this blog, you had seen many operations which you can do. Next section you will see something more interesting about python dictionaries.

Conclusion:-

Python dictionary is very useful to access data. With the help of a python dictionary, you can store the data easily.

It is ordered, unchangeable and doesn’t allow duplicate items.

It has a feature of nesting dictionary to store multiple dictionaries in the parent dictionary.

You don’t need the indexing of the dictionary to check the position of value.

You can check it with the key.

Avatar for Vipul kunwar
Follow me

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top