1dictionary = {"message": "Hello, World!"}
2
3data = dictionary.get("message", "")
4
5print(data) # Hello, World!
6
1#The get() method in dictionary returns:
2#the value for the specified key if key is in dictionary.
3#None if the key is not found and value is not specified.
4#value if the key is not found and value is specified.
5# value is provided
6print('Salary: ', person.get('salary', 0.0))
1dict = {'color': 'blue', 'shape': 'square', 'perimeter':20}
2dict.get('shape') #returns square
3
4#You can also set a return value in case key doesn't exist (default is None)
5dict.get('volume', 'The key was not found') #returns 'The key was not found'
1my_dict = {'a': 1, 'b': 2, 'c': 3}
2# keys = list(item.keys())
3# the upper commented code give the list of the keys.
4keys = ['a', 'b', 'c', 'd'] # if some body give the list of the keys that wasn't match with my_dict
5
6for key in keys:
7 print(my_dict.get(key, 'default value when key not exist'))
8
9"""output:
101
112
123
13default value when key not exist
14"""
1#!/usr/bin/python
2
3dict = {'Name': 'Zabra', 'Age': 7}
4print "Value : %s" % dict.get('Age')
5print "Value : %s" % dict.get('Education', "Never")