Access Dictionary Values Given by User in Python
Dictionaries are a fundamental data structure in Python, providing a flexible way to store and retrieve data using key-value pairs. Accessing dictionary values is a common operation in programming, and Python offers various methods to accomplish this task efficiently. In this article, we will explore some generally used methods to access dictionary values given by the user in Python.
Example
Input: {'name': 'Beginner', 'age': 21, 'country': 'India'} Output : Enter a key: age The value for key 'age' is: 21
How to Access Dictionary Values Given by User in Python?
Below, are the methods of How to Access Dictionary Values Given by User in Python.
- Using Bracket Notation
- Using get() Method
- Using keys() Method
- Using items() Method
- Using Default Dictionary
Access Dictionary Values Given by User in Python Using Bracket Notation
In this example, the below code takes user input for a key, accesses the corresponding value from the dictionary `user_dict`, and prints it along with the key. Note that it may raise a `KeyError` if the entered key is not present in the dictionary.
Python3
user_dict = { 'name' : 'Beginner' , 'age' : 21 , 'country' : 'India' } key = input ( "Enter a key: " ) value = user_dict[key] print (f "The value for key '{key}' is: {value}" ) |
Output
Enter a key: age
The value for key 'age' is: 21
Access Dictionary Values Given by User in Python Using get() Method
In this example, below code, used user input to retrieve the value associated with the entered key from the dictionary `user_dict`. The `get()` method is employed to handle the case where the key is not found, providing a default value of “Key not found.” The result is then printed.
Python3
user_dict = { 'name' : 'Beginner' , 'age' : 21 , 'country' : 'India' } key = input ( "Enter a key: " ) value = user_dict.get(key, "Key not found" ) print (f "The value for key '{key}' is: {value}" ) |
Output
Enter a key: city
The value for key 'city' is: Key not found
Access Dictionary Values Given by User in Python Using keys() Method
In this example, below code prompts the user to input a key and checks if it exists in the dictionary `user_dict`. If found, it retrieves and prints the corresponding value; otherwise, it notifies the user that the key was not found in the dictionary.
Python3
user_dict = { 'name' : 'Beginner' , 'age' : 21 , 'country' : 'India' } key = input ( "Enter a key: " ) if key in user_dict.keys(): value = user_dict[key] print (f "The value for key '{key}' is: {value}" ) else : print (f "Key '{key}' not found in the dictionary." ) |
Output
Enter a key: name
The value for key 'name' is: Beginner
Access Dictionary Values Given by User in Python Using items() Method
In this example, below code allows the user to input a key and then iterates through the key-value pairs in the dictionary `user_dict`. If the entered key is found, it prints the corresponding value; otherwise, it notifies the user that the key was not found in the dictionary.
Python3
user_dict = { 'name' : 'Beginner' , 'age' : 21 , 'country' : 'India' } key = input ( "Enter a key: " ) for k, v in user_dict.items(): if k = = key: print (f "The value for key '{key}' is: {v}" ) break else : print (f "Key '{key}' not found in the dictionary." ) |
Output
Enter a key: age
The value for key 'age' is: 21
Access Dictionary Values Given by User in Python Using Default Dictionary
In this example ,In this code, the user is prompted to input a key, and a `defaultdict` with a default value of “Key not found” is created using the original dictionary `user_dict`. The value associated with the entered key is then retrieved from the `default_dict` and printed.
Python3
from collections import defaultdict user_dict = { 'name' : 'Beginner' , 'age' : 21 , 'country' : 'India' } key = input ( "Enter a key: " ) default_dict = defaultdict( lambda : "Key not found" , user_dict) value = default_dict[key] print (f "The value for key '{key}' is: {value}" ) |
Output
Enter a key: country
The value for key 'country' is: India
Conclusion
In Conclusion , Accessing dictionary values in Python is a common operation, and these five methods provide flexibility and options based on different scenarios. Choosing the appropriate method depends on the specific requirements of your code. Whether using basic bracket notation, get()
, keys()
, items()
, or defaultdict
, Python offers versatile tools to make dictionary value access efficient and error-resistant.
Contact Us