Home » Python

Python Dictionary get() Method with Example

Python Dictionary get() Method: Here, we are going to learn how to get the value of an element based on the specified key?
Submitted by IncludeHelp, on November 25, 2019

Dictionary get() Method

get() method is used to get the value of an element based on the specified key.

Syntax:

    dictionary_name.fromkeys(keys, value)

Parameter(s):

  • key – It represents the name of the key whose value to be returned.
  • value – It is an optional parameter, that is used to specify the value to be returned if item does not exist.

Return value:

The return type of this method is based on the element type, it returns the element on specified key, if key does not exist it returns "None", if we defined any value, it returns that value if key does not exist.

Example 1:

# Python Dictionary get() Method with Example

# dictionary declaration
student = {
  "roll_no": 101,
  "name": "Shivang",
  "course": "B.Tech",
  "perc" : 98.5
}

# printing dictionary
print("data of student dictionary...")
print(student)

# printing the value of "roll_no"
print("roll_no is:", student.get('roll_no'))

# printing the value of "name"
print("name is:", student.get('name'))

# printing the value of "course"
print("course is:", student.get('course'))

# printing the value of "perc"
print("perc is:", student.get('perc'))

# trying to get item of the key which does not exist
print("address is:", student.get('address'))

# trying to get item of the key which does not exist
# and returning some value if key does not exist
print("address is:", student.get('address', "does not exist."))

Output

data of student dictionary...
{'perc': 98.5, 'course': 'B.Tech', 'name': 'Shivang', 'roll_no': 101}
roll_no is: 101
name is: Shivang
course is: B.Tech
perc is: 98.5
address is: None
address is: does not exist.

Example 2:

# Python Dictionary get() Method with Example

# dictionary declaration
x = {
    'key1' : 100,
    'key2' : 200,
    'key3' : 300
}

# getting value with defining value if key
# does not exist
print("key1 = ", x.get('key1', "Item does not exist."))
print("key2 = ", x.get('key2', "Item does not exist."))
print("key3 = ", x.get('key3', "Item does not exist."))
print("key4 = ", x.get('key4', "Item does not exist."))

Output

key1 =  100
key2 =  200
key3 =  300
key4 =  Item does not exist.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.