Join Regular Classroom : Visit ClassroomTech

get() method in Python Dictionary

It is a method in python that returns the values of the specified key in the dictionary.

Syntax:

dictionary.get(key, default_value)
*default_value is optional. In case the key is not present, it returns the default value.

Don’t know what is copy method in Python? Read: copy() method in Python Dictionary

Example 1:

s = {0:"CodeWindow", 1:"India", 2:"News"}
x = s.get(1)
print(x)

Output:

India

Explanation:

Here it returned the value (India) of the specified key (1) in the dictionary.

Example 2:

s = {"site":"CodeWindow", "country":"India", "type":"News", "platform":"IT"}
x = s.get("type")
print(x)

Output:

News

Explanation:

Here it returned the value (News) of the specified value (“type”) in the dictionary.


You Missed

Also Checkout