Python | Iterate through value lists dictionary
While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind of problem can occur in web development domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension
List comprehension can be used to perform this particular task. It is just the shorthand to the conventional nested loops. we iterate for each key’s list and store the result.
# Python3 code to demonstrate working of # Iterating through value lists dictionary # Using list comprehension # Initialize dictionary test_dict = {'gfg' : [1, 2], 'is' : [4, 5], 'best' : [7, 8]} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Using list comprehension # Iterating through value lists dictionary res = [[i for i in test_dict[x]] for x in test_dict.keys()] # printing result print("The list values of keys are : " + str(res)) |
The original dictionary : {‘best’: [7, 8], ‘is’: [4, 5], ‘gfg’: [1, 2]}
The list values of keys are : [[7, 8], [4, 5], [1, 2]]
Method #2 : Using from_iterable() + product() + items()
The combination of above functions can be used to perform this particular task. The from_iterable() can be used to reduce inner loop and items function is used to extract key value pairs in the dictionary.
# Python3 code to demonstrate working of # Iterating through value lists dictionary # Using from_iterable() + product() + items() import itertools # Initialize dictionary test_dict = {'gfg' : [1, 2], 'is' : [4, 5], 'best' : [7, 8]} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Iterating through value lists dictionary # Using from_iterable() + product() + items() res = [] for key, value in ( itertools.chain.from_iterable( [itertools.product((k, ), v) for k, v in test_dict.items()])): res.append(value) # printing result print("The list values of keys are : " + str(res)) |
The original dictionary : {‘best’: [7, 8], ‘is’: [4, 5], ‘gfg’: [1, 2]}
The list values of keys are : [7, 8, 1, 2, 4, 5]
Recommended Posts:
- Iterate over a dictionary in Python
- Python | Iterate over multiple lists simultaneously
- Python | Ways to iterate tuple list of lists
- Python | Concatenate dictionary value lists
- Python | Convert two lists into a dictionary
- Python | Ways to create a dictionary of Lists
- Python | Initializing dictionary with empty lists
- Python | Convert list of tuples to dictionary value lists
- Python | Split dictionary of lists to list of dictionaries
- Python | Split dictionary keys and values into separate lists
- Iterate over a set in Python
- Iterate over a list in Python
- Iterate over characters of a string in Python
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



