A dictionary is quite a useful data structure in Python programming that is usually used to hash a particular key with value so that it can be retrieved efficiently.
To access an item in the dictionary, refer to its key name inside square brackets.
Example
Python3
dict = {
"country" : "India" ,
"continent" : "Asia" ,
"Other_name" : "Bharat"
}
x = dict [ "continent" ]
print (x)
|
Output
Asia
We used a very basic and default method to access a dictionary item using it’s key. We will discuss more advanced methods to access value in dictionary.
How to Access Values in Dictionary
There are various ways to access items in the Dictionary or call dictionary value, we are explaining some generally used and easy methods we use for accessing Items (key-value) in the dictionary:
- Using key() Method
- Using values() Method
- Using an operator
- Using List Comprehension
- Using dict.items()
- Using enumerate() Method
- Using for loop and items() method
Printing Dictionary Using print() and str() Functions
In this example, we call dictionary value. Below Python code initializes a dictionary named test_dict with key-value pairs. It then prints the original dictionary using the print function and the str() function for converting the dictionary to a string for display purposes.
Python3
test_dict = { "Geeks" : 1 , "for" : 2 , "geeks" : 3 }
print ( "Original dictionary is : " + str (test_dict))
|
Output :
Original dictionary is : {'Geeks': 1, 'for': 2, 'geeks': 3}
1. Access Items in Dictionary using key() Method
In Python dictionaries, the keys() method returns a view of the keys. By iterating through this view using a for loop, you can access keys in the dictionary efficiently. This approach simplifies key-specific operations without the need for additional methods.
Example: In this example below code iterates through the keys of the test_dict dictionary and prints each key along with its corresponding value.
Python3
for key in test_dict.keys():
print ( "Key:" , key, "Value:" , test_dict[key])
|
Output :
Key: Geeks Value: 1Key: for Value: 2Key: geeks Value: 3
Time Complexity :O(n)
Space Complexity : O(n)
2. Access the Value in Dictionary using values() Method
In Python, we can access the values in a dictionary using the values() method. This method returns a view of all values in the dictionary, allowing you to iterate through them using a for loop or convert them to a list.
Example: In this example, the code iterates through the values of the test_dict dictionary and prints each value.
Python3
for value in test_dict.values():
print ( "Value:" , value )
|
Output :
Value: 1Value: 2Value: 3
Time complexity :O(n)
Space Compexity : O(n)
3. Access Dictionary Items using ‘in’ Operator
The most used method that can get all the keys along with its value, the “in” operator is widely used for this very purpose and highly recommended as it offers a concise method to achieve this task.
Example: In the example below Python code prints the key-value pairs of a dictionary using the `in` operator. It initializes a dictionary, iterates through its keys, and prints each key along with its corresponding value.
Python3
print ( "Dict key-value are : " )
for i in test_dict:
print (i, test_dict[i])
|
Output:
Original dictionary is : {'geeks': 3, 'for': 2, 'Geeks': 1}
Dict key-value are :
geeks 3
for 2
Geeks 1
Time Complexity: O(n)
Auxiliary Space: O(1)
4. Access a Dictionary Items using List Comprehension
This method also uses a method similar to the above method, it just binds the logic into one list and returns the key-value pairs of a dictionary as tuples of key and value in the list.
Example: In this example the below code utilizes dictionary comprehension to convert a dictionary into a list of key-value pairs using list comprehension, demonstrating the original dictionary and the resulting list of tuples.
Python3
print ( "Dict key-value are : " )
print ([(k, test_dict[k]) for k in test_dict])
|
Output:
Original dictionary is : {'Geeks': 1, 'for': 2, 'geeks': 3}
Dict key-value are :
[('Geeks', 1), ('for', 2), ('geeks', 3)]
Time Complexity: O(n)
Auxiliary Space: O(1)
5. Access Items in a Dictionary Using dict.items()
Python dictionary items() method iterates over all the keys and helps us to access the key-value pair one after the other in the loop and is also a good method to access dictionary keys with value.
Example: In this example, the below code demonstrates how to retrieve both keys and values from a dictionary in Python. It initializes a dictionary, prints the original dictionary, and then uses a loop with `dict.items()` to print each key-value pair in the dictionary.
Python3
print ( "Dict key-value are : " )
for key, value in test_dict.items():
print (key, value)
|
Output:
Original dictionary is : {'geeks': 3, 'for': 2, 'Geeks': 1}
Dict key-value are :
geeks 3
for 2
Geeks 1
Time Complexity: O(n)
Auxiliary Space: O(1)
6. Access Items in Dictionary using enumerate()
enumerate() in Python helps to iterate over all kinds of containers, be it a dictionary or a list. The power of this function can also be utilized to perform this task. It also additionally helps to access the named index of the position of the pair in the dictionary.
Example: In this example, the below code uses Python’s `enumerate()` function to iterate over the key-value pairs of a dictionary (`test_dict`). It prints the original dictionary and then uses `enumerate()` with `items()` to display the key-value pairs along with their indices.
Python3
print ( "Dict key-value are : " )
for i in enumerate (test_dict.items()):
print (i)
|
Output:
Original dictionary is : {'geeks': 3, 'Geeks': 1, 'for': 2}
Dict key-value are :
(0, ('geeks', 3))
(1, ('Geeks', 1))
(2, ('for', 2))
Time Complexity: O(n)
Auxiliary Space: O(1)
6. Access Values of the Dictionary using For loop and Items() Method
In Python, you can access items in a dictionary using a for loop and the items() method. The loop iterates over key-value pairs, allowing you to process both keys and corresponding values. This concise approach simplifies dictionary traversal, enhancing code readability and efficiency.
Example: In this example, the below code initializes a dictionary, prints it, and then extracts key-value pairs using a for loop and `items()`. The pairs are stored in a list and printed, showcasing the iteration over dictionary items for access and manipulation.
Python3
key_value_pairs = []
for key, value in test_dict.items():
key_value_pairs.append((key, value))
print ( "Dict key-value are : " )
for pair in key_value_pairs:
print (pair)
|
Output:
Original dictionary is : {'Geeks': 1, 'for': 2, 'geeks': 3}
Dict key-value are :
('Geeks', 1)
('for', 2)
('geeks', 3)
Time complexity :O(n)
Space Complexity : O(n)
We have covered easy methods on “How to access values in dictionary”, you can easily access the dictionary values using these methods. Accessing values in a dictionary is very important for data manipulation, updation, deletion, etc.
Also Read:
Please Login to comment...