Python Get Dictionary Value by Key
Below, are the methods of Python Get Dictionary Value by Key in Python.
- Using Bracket Notation
- Using get() Method
- Using setdefault() Method
- Using Collections Module
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
# Creating a sample dictionary sample_dict = { 'name' : 'geeks' , 'age' : 21 , 'place' : 'India' } # Accessing value using bracket notation name_value = sample_dict[ 'name' ] print ( "Name:" , name_value) |
Name: geeks
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
# Creating a sample dictionary sample_dict = { 'name' : 'geeks' , 'age' : 21 , 'place' : 'India' } # Using get() method name_value = sample_dict.get( 'name' ) print ( "Name:" , name_value) |
Name: geeks
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
# Creating a sample dictionary sample_dict = { 'name' : 'geeks' , 'age' : 21 , 'place' : 'India' } # Using setdefault() method name_value = sample_dict.setdefault( 'name' , 'Default Name' ) print ( "Name:" , name_value) # Using setdefault() for a key not present in the dictionary country_value = sample_dict.setdefault( 'country' , 'Unknown' ) print ( "Country:" , country_value) |
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 # Creating a defaultdict with a default value of 'Unknown' sample_dict = defaultdict( lambda : 'Unknown' , { 'name' : 'geeks' , 'age' : 21 , 'place' : 'India' } ) # Accessing value using bracket notation country_value = sample_dict[ 'place' ] print ( "Country:" , country_value) |
Country: India
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.
Python Get Dictionary Value by Key
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.