Dictionaries are the implementation by Python of a data structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair.
The list of key value pairs in curly braces that is separated by comma defines a dictionary. Column ‘:’ separates the value of each key.
A dictionary cannot be sorted only to get a representation of the sorted dictionary. Inherently, dictionaries are orderless, but not other types, including lists and tuples. Therefore, you need an ordered data type, which is a list—probably a list of tuples.
Given a dictionary with the values as list, the task is to iterate over the dictionary and print it.
Examples:
Input:
dictionary = {
'hello': [110, 200, 300],
'this': [3, 4, 5],
'is': 10,
'BTechGeeks': [1, 2, 3, 'python']
}Output:
hello : 110 200 300 this : 3 4 5 is : 10 BTechGeeks : 1 2 3 python
Traverse the dictionary with list values
We can traverse the dictionary by many ways two of them are
Method #1:Using nested for loop and exceptional handling
Approach:
- Iterate over the keys of dictionary.
- Now using nested for loop iterate over the values of keys.
- If a key has only one value, we get an error, so handle those errors with exceptional handling in Python by printing the key’s single value.
Below is the implementation:
# given dictionary
dictionary = {
'hello': [110, 200, 300],
'this': [3, 4, 5],
'is': 10,
'BTechGeeks': [1, 2, 3, 'python']
}
# iterate through dictionary
for key in dictionary:
print(key, end=" : ")
try:
for value in dictionary[key]:
print(value, end=" ")
except:
print(dictionary[key], end=" ")
print()
Output:
hello : 110 200 300 this : 3 4 5 is : 10 BTechGeeks : 1 2 3 python
Method #2:Using List comprehension
In the preceding example, we iterated through all of the list values for each key. However, if you want a complete list of pairs, we can also use list comprehension. Because a key in our dictionary can have multiple values, for each pair we will generate a list of pairs where the key is the same but the value is different.
Below is the implementation:
# given dictionary
dictionary = {
'hello': [110, 200, 300],
'this': [3, 4, 5],
'is': [2, 10, 45],
'BTechGeeks': [1, 2, 3, 'python']
}
# using list comprehension
keyvaluepairs = [(key, value)
for key, values in dictionary.items()
for value in values]
# traverse and print key value pairs
for i in keyvaluepairs:
print(i)
Output:
('hello', 110)
('hello', 200)
('hello', 300)
('this', 3)
('this', 4)
('this', 5)
('is', 2)
('is', 10)
('is', 45)
('BTechGeeks', 1)
('BTechGeeks', 2)
('BTechGeeks', 3)
('BTechGeeks', 'python')Related Programs:
- python iterate over dictionary with index
- python iterate loop over all nested dictionary values
- loop iterate over all values of dictionary in python
- python how to iterate over a list
- python different ways to iterate over a list in reverse order
- python how to create a list and initialize with same values
- python dictionary with multiple values per key
