Python Get Dictionary Value by Key
Last Updated :
16 Feb, 2024
Dictionaries are a fundamental data structure in Python, allowing you to store and retrieve data using key-value pairs. Retrieving a value from a dictionary by its key is a common operation in many Python programs. In this article, we will explore five simple and commonly used methods to get a dictionary value by its key in Python.
Python Get Dictionary Value by Key
Below, are the methods of Python Get Dictionary Value by Key in Python.
Python Get Dictionary Value by Key Using Bracket Notation
In this example, a sample dictionary named `sample_dict` is created with key-value pairs representing a person’s information. The value associated with the key ‘name‘ is then accessed using bracket notation, and the result is printed as “Name: geeks”.
Python3
sample_dict = { 'name' : 'geeks' , 'age' : 21 , 'place' : 'India' }
name_value = sample_dict[ 'name' ]
print ( "Name:" , name_value)
|
Python Get Dictionary Value by Key Using get() Method
In this example, a sample dictionary named `sample_dict` is created with key-value pairs. The `get()` method is then used to retrieve the value associated with the key ‘name‘ safely, and the result is printed as “Name: geeks”.
Python3
sample_dict = { 'name' : 'geeks' , 'age' : 21 , 'place' : 'India' }
name_value = sample_dict.get( 'name' )
print ( "Name:" , name_value)
|
Python Get Dictionary Value by Key Using setdefault() Method
In this example, a sample dictionary named `sample_dict` is created. The `setdefault()` method is used to retrieve the value associated with the key ‘name’ and set a default value (‘Default Name’) if the key is not found. Additionally, `setdefault()` is used for a key (‘country’) not present in the dictionary, setting a default value (‘Unknown’).
Python3
sample_dict = { 'name' : 'geeks' , 'age' : 21 , 'place' : 'India' }
name_value = sample_dict.setdefault( 'name' , 'Default Name' )
print ( "Name:" , name_value)
country_value = sample_dict.setdefault( 'country' , 'Unknown' )
print ( "Country:" , country_value)
|
Output
Name: geeks
Country: Unknown
Python Get Dictionary Value by Key Using Collections Module
In this example, a `defaultdict` named `sample_dict` is created from the `collections` module with a default value of ‘Unknown’. The value associated with the key ‘place’ is then accessed using bracket notation, and the result is printed as “Country: India”.
Python3
from collections import defaultdict
sample_dict = defaultdict( lambda : 'Unknown' , { 'name' : 'geeks' , 'age' : 21 , 'place' : 'India' }
)
country_value = sample_dict[ 'place' ]
print ( "Country:" , country_value)
|
Conclusion
In conclusion, Python offers multiple methods to retrieve dictionary values by key, each with its own advantages. Choose the method that best fits your needs based on the specific requirements of your program. Whether you prefer simplicity, safety, or additional functionality, Python’s rich set of tools allows you to work efficiently with dictionaries.
Share your thoughts in the comments
Please Login to comment...