python dictionary get with param code example
Example 1: get() python
# The get() method on dicts
# and its "default" argument
name_for_userid = {
382: "Alice",
590: "Bob",
951: "Dilbert",
}
def greeting(userid):
return "Hi %s!" % name_for_userid.get(userid, "there")
>>> greeting(382)
"Hi Alice!"
>>> greeting(333333)
"Hi there!"
'''When "get()" is called it checks if the given key exists in the dict.
If it does exist, the value for that key is returned.
If it does not exist then the value of the default argument is returned instead.
'''
# transferred by @ebdeuslave
# From Dan Bader - realpython.com
Example 2: how to get value from key dict in python
# Get a value from a dictionary in python from a key
# Create dictionary
dictionary = {1:"Bob", 2:"Alice", 3:"Jack"}
# Retrieve value from key 2
entry = dictionary[2]
# >>> entry
# Alice