Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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




# 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:

  • Import the defaultdict module from the collections library.
  • Define a list of dictionaries called test_list, where each dictionary contains keys and values. Each key represents a string, and each value is a list of integers.
  • Print the original list using the print() function.
  • Create an empty defaultdict object called result, which will be used to store the concatenated dictionary.
  • Use a for loop to iterate over each dictionary in the test_list. The loop variable i will be used to index each dictionary in the list.
  • Within the for loop, assign the current dictionary being iterated over to the variable current.
  • Use a nested for loop to iterate over each key-value pair in the current dictionary. The loop variables key and value will be used to store the key and value pairs of the dictionary being iterated over.
  • Within the nested for loop, use another for loop to iterate over each integer in the value list. The loop variable j will be used to index each integer in the list.
  • Append each integer in the value list to the corresponding key in the result dictionary. If the key does not exist in the result dictionary, it will be created and initialized as an empty list.
  • Print the concatenated dictionary using the dict() function to convert the defaultdict object to a regular dictionary, and the print() function to display the resulting dictionary.

Below is the implementation of the above approach:

Python3




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:

  • Create an empty dictionary res.
  • Loop through each dictionary in test_list.
    • For each dictionary, loop through each key-value pair using the items() method.
      • If the key is not in res, add it to res with the corresponding value.
      • If the key is already in res, concatenate the values using the + operator and update the corresponding key in res.
  • Return the resulting dictionary res.

Below is the implementation of the above approach:

Python3




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
  • Defining a function to concatenate the lists with similar keys
  • Applying the reduce function to concatenate the dictionaries
  • Printing the result

Python3




# 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:

  • Importing the itertools module.
  • Initializing the list of dictionaries.
  • Using the chain.from_iterable() function from itertools to create a single list of all values for each key in the list of dictionaries.
  • Using the set() function to create a set of all keys in the list of dictionaries.
  • Using a dictionary comprehension to create a new dictionary with each key and its concatenated list of values.
  • Printing the result.

Below is the implementation of the above approach:

Python3




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


My Personal Notes arrow_drop_up
Last Updated : 12 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials