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)) `

Output

```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

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)) `

Output

```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`

Output

```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)

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))`

Output

```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.

Previous
Next