The get() method in python is commonly used with dictionaries and can be used with other types like lists or custom classes.
The get() method is versatile and primarily used to safety access dictionary values without raising errors for missing keys. It can be adapted for other data structures as needed.
get() method with dictionaries
In python Dictionaries, the ‘get()’ method retrieves the value for a given key if the key is present in the dictionary.
If the key is not found, it returns a default value(which is ‘None’ if not specified).
my_dict = {'name': 'Tuppu', 'age': 25, 'location': 'hyd'} name = my_dict.get('name') age = my_dict.get('age', 0) gender = my_dict.get('gender', 'Unknown') print(name, age, gender)
output:
Tuppu 25 Unknown