Python | Concatenate dictionary value lists
Last Updated :
27 Apr, 2023
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
test_dict = {"Gfg" : [ 4 , 5 ], " is " : [ 6 , 8 ], "best" : [ 10 ]}
print ("The original dictionary is : " + str (test_dict))
res = sum (test_dict.values(), [])
print ("The Concatenated list values are : " + str (res))
|
Output :
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
from itertools import chain
test_dict = {"Gfg" : [ 4 , 5 ], " is " : [ 6 , 8 ], "best" : [ 10 ]}
print ("The original dictionary is : " + str (test_dict))
res = list (chain( * test_dict.values()))
print ("The Concatenated list values are : " + str (res))
|
Output :
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
test_dict = { "Gfg" : [ 4 , 5 ], "is" : [ 6 , 8 ], "best" : [ 10 ]}
print ( "The original dictionary is : " + str (test_dict))
x = []
for i in list (test_dict.values()):
x.extend(i)
print ( "The Concatenated list values are : " + str (x))
|
Output
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
from functools import reduce
test_dict = { "Gfg" : [ 4 , 5 ], "is" : [ 6 , 8 ], "best" : [ 10 ]}
print ( "The original dictionary is : " + str (test_dict))
res = reduce ( lambda x,y: x + y, test_dict.values())
print ( "The Concatenated list values are : " + str (res))
|
Output
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
test_dict = { "Gfg" : [ 4 , 5 ], "is" : [ 6 , 8 ], "best" : [ 10 ]}
print ( "The original dictionary is : " + str (test_dict))
res = []
x = list (test_dict.values())
for i in x:
for j in i:
res.append(j)
print ( "The Concatenated list values are : " + str (res))
|
Output
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
Share your thoughts in the comments
Please Login to comment...