In python, dictionaries are containers which map one key to its value with access time complexity to be O(1). But in many applications, the user doesn’t know all the keys present in the dictionaries. In such instances, if user tries to access a missing key, an error is popped indicating missing keys.
Traceback (most recent call last): File "46a9aac96614587f5b794e451a8f4f5f.py", line 9, in print (d['c']) KeyError: 'c'
In the above example, no key named ‘c’ in dictionary, popped a runtime error. To avoid such conditions, and to make aware user that a particular key is absent or to pop a default message in that place, there are several methods to handle missing keys.
Method 1 : Using get()
get(key,def_val) method is useful when we have to check for the key. If the key is present, value associated with the key is printed, else the def_value passed in arguments is returned.
Method 2 : Using setdefault()
setdefault(key, def_value) works in a similar way as get(), but the difference is that each time a key is absent, a new key is created with the def_value associated to the key passed in arguments.
For implementation of above functions, click here.
Method 3 : Using defaultdict
“defaultdict” is a container that is defined in module named “collections“. It takes a function(default factory) as its argument. By default, default factory is set to “int” i.e 0. If a key is not present is defaultdict, the default factory value is returned and displayed. It has advantages over get() or setdefault().
- A default value is set at the declaration. There is no need to invoke the function again and again and pass similar value as arguments. Hence saving time.
- The implementation of defaultdict is faster than get() or setdefault().
The value associated with 'a' is : 1 The value associated with 'c' is : Key Not found
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Convert Dictionaries List to Order Key Nested dictionaries
- Python | Difference in keys of two dictionaries
- Python | Intersect two dictionaries through keys
- Python - Compare Dictionaries on certain Keys
- Python - Concatenate values with same keys in a list of dictionaries
- Combine keys in a list of dictionaries in Python
- Python - Extract Maximum Keys' value dictionaries
- Python program to extract N largest dictionaries keys
- Python program to divide dictionary and its keys into K equal dictionaries
- Python - Extract selective keys' values Including Nested Keys
- Different ways of sorting Dictionary by Keys and Reverse sorting by keys
- Python | Sort Python Dictionaries by Key or Value
- Get() method for dictionaries in Python
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Python | Common items among dictionaries
- Python | Set Difference in list of dictionaries
- Python | Split dictionary of lists to list of dictionaries
- Python | Removing dictionary from list of dictionaries
- Python | Combine the values of two dictionaries having same key