Sometimes, while working with dictionaries, we might have a problem in which we have lists as it’s value and wish to have it cumulatively in single list by concatenation. This problem can occur in web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using sum() + values() This is the most recommended method and one liner to perform this task. In this, we access all list values using values() and concatenation utility is performed using sum().
Python3
# Python3 code to demonstrate working of # Concatenating dictionary value lists # Using sum() + values() # initializing dictionary test_dict = {"Gfg" : [ 4 , 5 ], " is " : [ 6 , 8 ], "best" : [ 10 ]} # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Concatenating dictionary value lists # Using sum() + values() res = sum (test_dict.values(), []) # printing result print ("The Concatenated list values are : " + str (res)) |
The original dictionary is : {'Gfg': [4, 5], 'best': [10], 'is': [6, 8]} The Concatenated list values are : [4, 5, 10, 6, 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 chain() + * operator This task can also be performed using the combination of these methods. In these, we just use inbuilt function of chain for concatenation to list and * operator is used to access all the list values into one.
Python3
# Python3 code to demonstrate working of # Concatenating dictionary value lists # Using chain() + * operator from itertools import chain # initializing dictionary test_dict = {"Gfg" : [ 4 , 5 ], " is " : [ 6 , 8 ], "best" : [ 10 ]} # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Concatenating dictionary value lists # Using chain() + * operator res = list (chain( * test_dict.values())) # printing result print ("The Concatenated list values are : " + str (res)) |
The original dictionary is : {'Gfg': [4, 5], 'best': [10], 'is': [6, 8]} The Concatenated list values are : [4, 5, 10, 6, 8]
Method #3 : Using extend(),list() and values() methods
Python3
# Python3 code to demonstrate working of # Concatenating dictionary value lists # initializing dictionary test_dict = { "Gfg" : [ 4 , 5 ], "is" : [ 6 , 8 ], "best" : [ 10 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Concatenating dictionary value lists x = [] for i in list (test_dict.values()): x.extend(i) # printing result print ( "The Concatenated list values are : " + str (x)) |
The original dictionary is : {'Gfg': [4, 5], 'is': [6, 8], 'best': [10]} The Concatenated list values are : [4, 5, 6, 8, 10]
Method #4 : Using reduce() method
Python3
# Python3 code to demonstrate working of # Concatenating dictionary value lists # Using reduce() and values() # importing reduce from functools from functools import reduce # initializing dictionary test_dict = { "Gfg" : [ 4 , 5 ], "is" : [ 6 , 8 ], "best" : [ 10 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Concatenating dictionary value lists # Using reduce() and values() res = reduce ( lambda x,y: x + y, test_dict.values()) # printing result print ( "The Concatenated list values are : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The original dictionary is : {'Gfg': [4, 5], 'is': [6, 8], 'best': [10]} The Concatenated list values are : [4, 5, 6, 8, 10]
In this example, We’ve used the reduce() function to iterate over the values of the dictionary and to concatenate them together. The lambda function defines how the lists are being concatenated together.
The time complexity of this approach would be O(n) where n is the number of items in the dictionary and space complexity would be O(n) as well, as we are creating a new list with all the items from the dictionary values.
Method #5 : Using nested for loop and append() method
Approach
- Access values list using values() method
- Initiate a nested for loop to append each element of list within values list to output list using append()
- Display output list
Python3
# Python3 code to demonstrate working of # Concatenating dictionary value lists # initializing dictionary test_dict = { "Gfg" : [ 4 , 5 ], "is" : [ 6 , 8 ], "best" : [ 10 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Concatenating dictionary value lists res = [] x = list (test_dict.values()) for i in x: for j in i: res.append(j) # printing result print ( "The Concatenated list values are : " + str (res)) |
The original dictionary is : {'Gfg': [4, 5], 'is': [6, 8], 'best': [10]} The Concatenated list values are : [4, 5, 6, 8, 10]
Time Complexity : O(M*N) M- length of values list N – length of each list in value list
Auxiliary Space : O(M*N) M- length of values list N – length of each list in value list