Contents
Python – Accessing Dictionaries
Dictionary is a collection of items where each item is a key-value pair.
So, you can access the dictionary items, or its keys, or its values.
1. Accessing a specific key-value in dictionary in Python
You can access a specific key-value pair in dictionary using a key.
If key_1 is the key for which you would like to access the value in the dictionary my_dict, then use the following syntax.
The above expression returns the value for the specified key.
For example, in the following program, we take a dictionary my_dict, and access the value for the key ‘bar’, and print it to output.
Python Program
my_dict = {
'foo':12,
'bar':14
}
key_1 = "bar"
value_1 = my_dict[key_1]
print(f"Value for the key \"{key_1}\" is {value_1}.")
Run Code CopyOutput
Value for the key "bar" is 14.
We can also set a new value using the same expression specified above, using assignment operator.
Now, let us set a new value for the key “bar”.
Python Program
my_dict = {
'foo':12,
'bar':14
}
key_1 = "bar"
my_dict[key_1] = "99"
print(my_dict)
Run Code CopyOutput
{'foo': 12, 'bar': '99'}
This is how we access a specific key-value pair in a dictionary, given the key.
2. Accessing dictionary items
You can access the items in the given dictionary using dictionary items() method. The items() method returns an iterator for the items in the dictionary, and you can use this iterator in a For loop, to traverse through the items and access them, or convert to a list and access the items in dictionary using index.
For example, in the following program, we take a dictionary my_dict, and access the items in this dictionary using items() method in a For loop.
During each iteration of the For loop, we get access to the key and value of the item.
Python Program
my_dict = {
'foo':12,
'bar':14
}
for key, value in my_dict.items():
print(key, value)
Run Code CopyOutput
foo 12
bar 14
Now, let us convert the dictionary items into a list, and access them using index.
Python Program
my_dict = {
'foo':12,
'bar':14
}
items_list = list(my_dict.items())
print(items_list[0])
print(items_list[1])
Run Code CopyOutput
('foo', 12)
('bar', 14)
Each item in the list is a tuple of key and value.
3. Accessing dictionary keys
You can access the keys alone from a given dictionary using dictionary keys() method. The keys() method returns an iterator for the keys in the dictionary, and you can use this iterator in a For loop, to traverse through the keys and access them, or convert to a list and access the individual keys in the dictionary using index.
For example, in the following program, we take a dictionary my_dict, and access the keys in this dictionary in a For loop.
During each iteration of the For loop, we get access to the key of an item.
Output
foo
bar
Now, let us convert the dictionary keys into a list, and access them using index.
Python Program
my_dict = {
'foo':12,
'bar':14
}
keys_list = list(my_dict.keys())
print(f"first key : {keys_list[0]}")
print(f"second key : {keys_list[1]}")
Run Code CopyOutput
first key : foo
second key : bar
4. Accessing dictionary values
You can also access the values alone from a given dictionary using dictionary values() method. The values() method returns an iterator for the values in the dictionary, and you can use this iterator in a For loop, to traverse through the values and access them, or convert to a list and access the individual values in the dictionary using index.
For example, in the following program, we take a dictionary my_dict, and access the values in this dictionary in a For loop.
During each iteration of the For loop, we get access to the value of an item.
Python Program
my_dict = {
'foo':12,
'bar':14
}
for value in my_dict.values():
print(value)
Run Code CopyOutput
12
14
Now, let us convert the dictionary keys into a list, and access them using index.
Python Program
my_dict = {
'foo':12,
'bar':14
}
values_list = list(my_dict.values())
print(f"first value : {values_list[0]}")
print(f"second value : {values_list[1]}")
Run Code CopyOutput
first value : 12
second value : 14
Summary
In this tutorial, we learned how to access a dictionary using a given key, access dictionary items in a loop or access the items individually using items() method, access dictionary keys, access dictionary values, with help of well detailed Python programs.