Last Updated : 27 Apr, 2023
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 comprehensionList 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
# 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 : {'gfg': [1, 2], 'is': [4, 5], 'best': [7, 8]} The list values of keys are : [[1, 2], [4, 5], [7, 8]]
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
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
# 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 : {'gfg': [1, 2], 'is': [4, 5], 'best': [7, 8]} The list values of keys are : [1, 2, 4, 5, 7, 8]Method #3 : Using map() + lambda
The combination of above functions can be used to perform this particular task. The map() can be used to perform certain operation on each elements in the list and lambda function can be used to perform the task.
Python3
# Python3 code to demonstrate working of
# Iterating through value lists dictionary
# Using map() + lambda
# 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 map() + lambda
res = []
for i in test_dict.values():
res.append(list(map(lambda x: x, i)))
# printing result
print("The list values of keys are : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
The original dictionary : {'gfg': [1, 2], 'is': [4, 5], 'best': [7, 8]} The list values of keys are : [[1, 2], [4, 5], [7, 8]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Step-by-step approach:
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of
# Iterating through value lists dictionary
# Using for loop + 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))
# Iterating through value lists dictionary
# Using for loop + list comprehension
res = []
for val in test_dict.values():
res.append([x for x in val])
# printing result
print("The list values of keys are : " + str(res))
The original dictionary : {'gfg': [1, 2], 'is': [4, 5], 'best': [7, 8]} The list values of keys are : [[1, 2], [4, 5], [7, 8]]
Time complexity: O(nm), where n is the number of keys in the dictionary and m is the maximum length of the value lists.
Auxiliary space: O(nm), to store the 'res' list.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4