Open In App

Python – Concatenate values with same keys in a list of dictionaries

Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform concatenation of all the key values list that is like in dictionary list. This is quite a common problem and has applications in domains such as day-day programming and web development domain. Let’s discuss the certain ways in which this task can be performed.

Method 1: Using loop 



This task can be performed using brute force way. In this we iterate for all the dictionaries and perform the concatenation of like keys by adding one list element to other on the key match.




# Python3 code to demonstrate working of
# Concatenate Similar Key values
# Using loop
 
# initializing list
test_list = [{'gfg': [1, 5, 6, 7], 'good': [9, 6, 2, 10],
              'CS': [4, 5, 6]},
             {'gfg': [5, 6, 7, 8], 'CS': [5, 7, 10]},
             {'gfg': [7, 5], 'best': [5, 7]}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Concatenate Similar Key values
# Using loop
res = dict()
for dict in test_list:
    for list in dict:
        if list in res:
            res[list] += (dict[list])
        else:
            res[list] = dict[list]
 
# printing result
print("The concatenated dictionary : " + str(res))

Output

The original list is : [{'gfg': [1, 5, 6, 7], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6]}, {'gfg': [5, 6, 7, 8], 'CS': [5, 7, 10]}, {'gfg': [7, 5], 'best': [5, 7]}]
The concatenated dictionary : {'gfg': [1, 5, 6, 7, 5, 6, 7, 8, 7, 5], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6, 5, 7, 10], 'best': [5, 7]}

Time complexity: O(n*m).
Auxiliary space: O(k).

Method 2: Using defaultdict

This program creates a concatenated dictionary from a list of dictionaries. It first initializes an empty defaultdict object, result, which will store the concatenated dictionary. It then iterates over each dictionary in the input list, and for each key-value pair in each dictionary, appends the value to the corresponding key in the result dictionary. Finally, it prints the original list and the concatenated dictionary.

Follow the below steps to implement the above idea:

Below is the implementation of the above approach:




from collections import defaultdict
test_list = [{'gfg' : [1, 5, 6, 7], 'good' : [9, 6, 2, 10],
              'CS' : [4, 5, 6]},
             {'gfg' : [5, 6, 7, 8], 'CS' : [5, 7, 10]},
             {'gfg' : [7, 5], 'best' : [5, 7]}]
 
print("Original List: " + str(test_list))
result = defaultdict(list)
 
for i in range(len(test_list)):
    current = test_list[i]
    for key, value in current.items():
        for j in range(len(value)):
            result[key].append(value[j])
 
print("Concatenated Dictionary: " + str(dict(result)))

Output
Original List: [{'gfg': [1, 5, 6, 7], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6]}, {'gfg': [5, 6, 7, 8], 'CS': [5, 7, 10]}, {'gfg': [7, 5], 'best': [5, 7]}]
Concatenated Dictionary: {'gfg': [1, 5, 6, 7, 5, 6, 7, 8, 7, 5], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6, 5, 7, 10], 'best': [5, 7]}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Using a dictionary comprehension.

Step-by-step approach:

Below is the implementation of the above approach:




test_list = [{'gfg': [1, 5, 6, 7], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6]},
             {'gfg': [5, 6, 7, 8], 'CS': [5, 7, 10]},
             {'gfg': [7, 5], 'best': [5, 7]}]
 
res = {}
for d in test_list:
    for k, v in d.items():
        res[k] = res.get(k, []) + v
 
print(res)

Output
{'gfg': [1, 5, 6, 7, 5, 6, 7, 8, 7, 5], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6, 5, 7, 10], 'best': [5, 7]}

Time complexity: O(N*M), where N is the number of dictionaries in test_list and M is the number of key-value pairs in each dictionary.
Auxiliary space: O(K), where K is the number of unique keys in test_list.

Method 4: using the reduce() function from the functools module.

Uses the reduce() function to apply the concatenate_dict() function to all dictionaries in the test_list, which concatenates the values of similar keys. The concatenate_dict() function takes two dictionaries and returns a dictionary with concatenated values. The reduce() function uses this function to reduce the list of dictionaries to a single concatenated dictionary.




# Importing the reduce function from functools module
from functools import reduce
 
# Initializing the list of dictionaries
test_list = [{'gfg': [1, 5, 6, 7], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6]},
             {'gfg': [5, 6, 7, 8], 'CS': [5, 7, 10]},
             {'gfg': [7, 5], 'best': [5, 7]}]
 
# Defining a function to concatenate the lists with similar keys
def concatenate_dict(d1, d2):
    for key in d2:
        if key in d1:
            d1[key] += d2[key]
        else:
            d1[key] = d2[key]
    return d1
 
# Applying the reduce function to concatenate the dictionaries
res = reduce(concatenate_dict, test_list)
 
# Printing the result
print("The concatenated dictionary: " + str(res))

Output
The concatenated dictionary: {'gfg': [1, 5, 6, 7, 5, 6, 7, 8, 7, 5], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6, 5, 7, 10], 'best': [5, 7]}

Time complexity: O(nk), where n is the number of dictionaries in test_list and k is the average number of keys per dictionary. 
Auxiliary space: O(nk) because it requires creating a new dictionary to store the concatenated values for each key.

Method 5: Using the itertools module.

Step-by-step approach:

Below is the implementation of the above approach:




# Importing the itertools module
import itertools
 
# Initializing the list of dictionaries
test_list = [{'gfg': [1, 5, 6, 7], 'good': [9, 6, 2, 10], 'CS': [4, 5, 6]},
             {'gfg': [5, 6, 7, 8], 'CS': [5, 7, 10]},
             {'gfg': [7, 5], 'best': [5, 7]}]
 
# Using chain.from_iterable() to create a single list of all values for each key
concatenated_lists = {key: list(itertools.chain.from_iterable([d.get(key, []) for d in test_list])) for key in set().union(*test_list)}
 
# Printing the result
print("The concatenated dictionary: " + str(concatenated_lists))

Output
The concatenated dictionary: {'CS': [4, 5, 6, 5, 7, 10], 'good': [9, 6, 2, 10], 'gfg': [1, 5, 6, 7, 5, 6, 7, 8, 7, 5], 'best': [5, 7]}

Time complexity: O(nm), where n is the number of dictionaries in the list and m is the total number of keys in all dictionaries.
Auxiliary space: O(m), where m is the total number of keys in all dictionaries. 


Article Tags :