Python | Iterate through value lists dictionary
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 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
# 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))
OutputThe 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
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
# 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))
OutputThe 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
OutputThe 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)
Method 4: Using a for loop to iterate over the values of the dictionary and using a list comprehension to create a new list for each value.
Step-by-step approach:
- Initialize an empty list 'res' to store the results.
- Iterate over the values of the dictionary using a for loop.
- Use a list comprehension to create a new list for each value.
- Append the new list to the 'res' list.
- Print the 'res' list.
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))
OutputThe 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.
Similar Reads
Iterate Through Dictionary Keys And Values In Python In Python, a Dictionary is a data structure where the data will be in the form of key and value pairs. So, to work with dictionaries we need to know how we can iterate through the keys and values. In this article, we will explore different approaches to iterate through keys and values in a Dictionar
2 min read
Python Iterate Dictionary Key, Value In Python, a Dictionary is a data structure that stores the data in the form of key-value pairs. It is a mutable (which means once created we modify or update its value later on) and unordered data structure in Python. There is a thing to keep in mind while creating a dictionary every key in the dic
3 min read
Get Python Dictionary Values as List - Python We are given a dictionary where the values are lists and our task is to retrieve all the values as a single flattened list. For example, given the dictionary: d = {"a": [1, 2], "b": [3, 4], "c": [5]} the expected output is: [1, 2, 3, 4, 5]Using itertools.chain()itertools.chain() function efficiently
2 min read
Inverse Dictionary Values List - Python We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]}Using defaul
2 min read
Python - Value Dictionary from Record List Sometimes, while working with Python Records lists, we can have problems in which, we need to reform the dictionary taking just values of the binary dictionary. This can have applications in many domains which work with data. Let us discuss certain ways in which this task can be performed. Method #1
6 min read