This is a text field

Python Dictionary- 5 Methods to Get keys or values!

In the previous blog python-dictionary, you had seen the basics of python dictionary.

You had seen why we use dictionaries? How we use it? and what is a dictionary? This all the basics things I have covered in the python-dictionary, blog.

In this blog, you are going to see the 5 methods which you will use to get keys or values in python.

These methods are useful when you want specified keys and values in the dictionary.

In this blog, you are going to see:-

•fromkeys() method
•get() method
•setdefault() method
•values()
•items() method

Dict- fromkeys() method

The fromkeys() method get specified key and specified values.

Why use it?

To get the dictionary which is the same value for all keys.

For example, the person registered in three sports at a time.

The sports in which he participates will be a dictionary and his name will be value in the dictionary.

How to use it?

You can use the fromkeys() method from syntax fromkeys(keys, value).

The keys which you will use to create a dictionary and the value which you will use for all keys.

For example,

Here, you can see in all three sports, the person name is the same.

Suppose, there is no one taking part in sports, which means the value is nothing. Then how will the dictionary look like?

Let’s see!

Value: None

Here, you can see that the value of all sports are None.because no one took part in sports.

Dict- get() method

The get() method return the value of a key in a dictionary.

Why use it?

Suppose, you want to get the value of a key from a dictionary. To gain the value using the key you will use get() method

How to do it?

You can do it with the syntax- variable.get(key).

For example,

Here, you can see that we had gained the value:- steve with the help of key:- name.

Dict- setdefault() method

The setdefault() method give the value of the key with the default value.

Why use it?

The setdefault() method giving the value with help of a key.

If the key is not present in the dictionary it will give the default value.

How to use it?

You can use setdefault() method with the syntax:- setdefault(key, default_value).

For example,

Here, you can see that the given key in the setdefault() method is present in the dictionary. So, the value came from the dictionary.

Suppose, the passed key not present in the dictionary then the default value will come.

For example,

Here, you can see that key “color” has passed. but it is not present in the dictionary. so the default value “vipul” has come.

Dict- values() method

The values() method return all the list of values from the dictionary.

Why use it?

To get the list of all values, not key. It’s helpful to change the data type of value and eliminate the key.

How to use it?

You can do it with the help of the values() method.

For example,

Here, you can see that list of all values has come.

Suppose, you have changed the value in the dictionary. What the impact of this on the values() method.

For example,

Here, you can see that after changing the value of the key name. The list of all keys had changed.

Dict- items() method

The items() method return the key-value pair as a tuple in the list.

Why use it?

To get the key-value pair in the list we using the items() method.

How to use it?

You can use it with the help of syntax- dictionary.items().

For example,

Here, you can see that the key-value pair as a tuple in a list has come.

Suppose, you have made certain changes in key-value. Then what will happen?

For example,

Here, you can see that after making changes in the key-value pair it reflected in the dictionary.

The age’s key-value pair has changed.

Summary:-

In this section, you had seen that how can you gain the key or value from the dictionary.

The 5 methods to do this are fromkeys(), get(), setdefault(), values() and items() method.

This method giving insight into how the data changing.

Here, you had also seen how you can gain only a list of values and keys.

                    LIKE ME AT

Avatar for Vipul kunwar
Follow me

    Leave a Reply

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

    Back to top