Python – Group single item dictionaries into List values

Given List of single item dictionaries, group them into dictionary value lists according to similar values. 

Input : [{“Gfg” : 3}, {“is”: 8}, {“Gfg”: 18}, {“Best”: 33}]
Output : {‘Gfg’: [3, 18], ‘is’: [8], ‘Best’: [33]}
Explanation : Each key converted to list values and dictionary.

Input : [{“Gfg” : 3}, {“Gfg”: 8}, {“Gfg”: 18}, {“Best”: 33}]
Output : {‘Gfg’: [3, 18, 8], ‘Best’: [33]}
Explanation : Each key converted to list values and dictionary.

 Method #1 : Using setdefault() + loop

This is brute way in which this task can be performed. In this, we use to loop through all the dictionary values and setdefault()  is used to assign common key its corresponding grouped value lists.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Group single item dictionaries into List values
# Using setdefault() + loop
  
# initializing lists
test_list = [{"Gfg" : 3}, {"is": 8}, {"Best": 10}, {"Gfg": 18}, {"Best": 33}]
  
# printing original list
print("The original list : " + str(test_list))
  
res = {}
  
# using loop to loop through each dictionary
for idx in test_list:
      
    # items() to extract item
    for key, val in idx.items():
          
        # setdefault performs task of setting empty list value as default
        res.setdefault(key, []).append(val)
  
# printing result 
print("The constructed dictionary : " + str(res))

chevron_right


Output

The original list : [{'Gfg': 3}, {'is': 8}, {'Best': 10}, {'Gfg': 18}, {'Best': 33}]
The constructed dictionary : {'Gfg': [3, 18], 'is': [8], 'Best': [10, 33]}

Method #2 : Using defaultdict() + * operator + loop

This is yet another way in which this task can be performed. In this, we use defaultdict() for empty list initialization. The * operator is used to unpack the dictionary item and loop is used to loop through dictionaries.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Group single item dictionaries into List values
# Using defaultdict() + * operator + loop
from collections import defaultdict
  
# initializing lists
test_list = [{"Gfg" : 3}, {"is": 8}, {"Best": 10}, {"Gfg": 18}, {"Best": 33}]
  
# printing original list
print("The original list : " + str(test_list))
  
res = defaultdict(list)
for ele in test_list:
      
    # using * operator to unpack
    # reducing one loop
    key, val = tuple(*ele.items())
    res[key].append(val)
  
# printing result 
print("The constructed dictionary : " + str(dict(res)))

chevron_right


Output

The original list : [{'Gfg': 3}, {'is': 8}, {'Best': 10}, {'Gfg': 18}, {'Best': 33}]
The constructed dictionary : {'Gfg': [3, 18], 'is': [8], 'Best': [10, 33]}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.