Python – Group Similar items to Dictionary Values List

Given a list of elements, perform grouping of similar elements, as different key-value list in dictionary.

Input : test_list = [4, 6, 6, 4, 2, 2, 4, 8, 5, 8]
Output : {4: [4, 4, 4], 6: [6, 6], 2: [2, 2], 8: [8, 8], 5: [5]}
Explanation : Similar items grouped together on occurrences.

Input : test_list = [7, 7, 7, 7]
Output : {7 : [7, 7, 7, 7]}
Explanation : Similar items grouped together on occurrences.

Method #1 : Using defaultdict() + loop

This is one of the ways in which this task can be performed. In this, we construct a defaultdict() with default list and keep appending similar values into similar list.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Group Similar items to Dictionary Values List
# Using defaultdict + loop
from collections import defaultdict
  
# initializing list
test_list = [4, 6, 6, 4, 2, 2, 4, 4, 8, 5, 8]
  
# printing original list
print("The original list : " + str(test_list))
  
# using defaultdict for default list 
res = defaultdict(list)
for ele in test_list:
      
    # appending Similar values
    res[ele].append(ele)
  
# printing result 
print("Similar grouped dictionary : " + str(dict(res)))

chevron_right


Output

The original list : [4, 6, 6, 4, 2, 2, 4, 4, 8, 5, 8]
Similar grouped dictionary : {4: [4, 4, 4, 4], 6: [6, 6], 2: [2, 2], 8: [8, 8], 5: [5]}

Method #2 : Using dictionary comprehension + Counter()

This is yet another way in which this task can be performed. In this, we extract frequency using Counter() and then repeat occurrences using multiplication.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Group Similar items to Dictionary Values List
# Using dictionary comprehension + Counter()
from collections import Counter
  
# initializing list
test_list = [4, 6, 6, 4, 2, 2, 4, 4, 8, 5, 8]
  
# printing original list
print("The original list : " + str(test_list))
  
# using * operator to perform multiplication
res = {key : [key] * val for key, val in Counter(test_list).items()}
  
# printing result 
print("Similar grouped dictionary : " + str(res))

chevron_right


Output

The original list : [4, 6, 6, 4, 2, 2, 4, 4, 8, 5, 8]
Similar grouped dictionary : {4: [4, 4, 4, 4], 6: [6, 6], 2: [2, 2], 8: [8, 8], 5: [5]}



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.