Traverse Dictionary in Python
Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary
In Python, dictionaries are a useful and commonly used data structure in which we have key-value pairs
Traversing a dictionary explains how to operate on a dictionary's key-value pairs while traversing through it.
There are many ways to traverse the dictionary in python
1. Using the items() method
2. Using the keys() method
3. Using the values() method
4. Using the Index to Iterate
1. Using the items() method
We can loop through the dictionary using Python's dict.items() method. You will learn the value and the key to each thing with each iteration.
Let ‘s see the examples using the items() method
Example 1
dictionary = {
'sravani': 'mamidgi',
'yamini': 'nallakunta',
'glory': 'parshapalli',
'chandana': 'ranabothu',
'shivapriya': 'thalla'
}
for keys, values in dictionary.items():
print(keys ,values)
output
sravanimamidgi
yamininallakunta
glory parshapalli
chandanaranabothu
shiva priyathalla
Example 2
dictionary = {
'Name': 'sravani',
'year': '2003',
'age': '19',
'locatiion': 'Zaheerabad'
}
for keys, values in dictionary.items():
print(keys ,values)
output
Name sravani
year 2003
age 19
locatiionZaheerabad
2. Using the keys() method
Use the keys() method that the dictionary provides to iterate through all of its keys. A list of the keys found in the dictionary is returned as an iterable.
Let ‘s see the examples using the items() method
Example 1
dictionary = {
'Name': 'sravani',
'year': '2003',
'age': '19',
'locatiion': 'Zaheerabad'
}
for keys in dictionary.keys():
print(keys)
output
Name
year
age
location
Example 2
dictionary = {
'sravani': 'mamidgi',
'yamini': 'nallakunta',
'glory': 'parshapalli',
'chandana': 'ranabothu',
'shivapriya': 'thalla'
}
for keys in dictionary.keys():
print(keys)
output
sravani
yamini
glory
chandana
shiva Priya
3. Using the values() method
Use the dictionary's values() method to loop through the values of the dictionary's elements.
The dictionary returns an iterable that contains all of the values for each entry. Then, by utilizing a for loop, you can cycle over the numbers as seen below.
Let ‘s see the examples using the items() method
Example 1
dictionary = {
'sravani': 'mamidgi',
'yamini': 'nallakunta',
'glory': 'parshapalli',
'chandana': 'ranabothu',
'shivapriya': 'thalla'
}
for values in dictionary.values():
print(values)
output
mamidgi
nallakunta
parshapalli
ranabothu
thalla
example 2
dictionary = {
'Name': 'sravani',
'year': '2003',
'age': '19',
'locatiion': 'Zaheerabad'
}
for values in dictionary.values():
print(values)
output
sravani
sravani
2003
19
Zaheerabad
4. Using the Index to Iterate
To iterate across the dictionary, use the item’s index. This is comparable to iterating the dictionary without using the methods keys(), values(), or items.
Let ‘s see the examples using the items() method
Example 1
dictionary = {
'Name': 'sravani',
'year': '2003',
'age': '19',
'locatiion': 'Zaheerabad'
}
for index in dictionary:
print(index, dictionary[index])
output
Name sravani
year 2003
age 19
locatiionZaheerabad
Example 2
dictionary = {
'sravani': 'mamidgi',
'yamini': 'nallakunta',
'glory': 'parshapalli',
'chandana': 'ranabothu',
'shivapriya': 'thalla'
}
for index in dictionary:
print(index, dictionary[index])
output
sravanimamidgi
yamininallakunta
glory parshapalli
chandanaranabothu
shiva priyathalla
Traversing over a dictionary in alphabetical order
Usually, dictionaries don't maintain any kind of structure. This suggests that the order of the elements in the iteration is not guaranteed. To iterate a dictionary using a specified order, use Python's sorted() method. The item will initially be sorted before being traversed through by a for a loop.
Example 1
Dictionary keys are sorted by the function sorted(dictionary.keys()), and for iterates through the keys that were returned.
Here is an example of iterating through an index-based dictionary
dictionary = {
'sravani': 'mamidgi',
'yamini': 'nallakunta',
'glory': 'parshapalli',
'chandana': 'ranabothu',
'shivapriya': 'thalla'
}
for keys in sorted(dictionary.keys()):
print(keys, dictionary[keys])
output
chandanaranabothu
glory parshapalli
shiva priyathalla
sravanimamidgi
yamininallakunta
Example 2
dictionary = {
'Name': 'sravani',
'year': '2003',
'age': '19',
'locatiion': 'Zaheerabad'
}
for keys in sorted(dictionary.keys()):
print(keys, dictionary[keys])
output
Name sravani
age 19
locatiionZaheerabad
year 2003
Combine the value and the key unlike using items()
In this illustration, a Python Loop Through a Dictionary is used, and with each iteration, the directory's key is obtained. Next, key data is printed, and a key is used as an index to display values from the directory.
Example 1
dictionary = {
'Name': 'sravani',
'year': '2003',
'age': '19',
'locatiion': 'Zaheerabad'
}
for i in dictionary:
print(i,'->',dictionary[i])
output
Name ->sravani
year -> 2003
age -> 19
locatiion ->Zaheerabad
example 2
dictionary = {
'sravani': 'mamidgi',
'yamini': 'nallakunta',
'glory': 'parshapalli',
'chandana': 'ranabothu',
'shivapriya': 'thalla'
}
for i in dictionary:
print(i,'->',dictionary[i])
output
sravani ->mamidgi
yamini ->nallakunta
glory ->parshapalli
chandana ->ranabothu
shiva priya ->thalla