Skip to content
Related Articles

Related Articles

Python – Concatenate all keys which have similar values

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 04 Sep, 2021

Given a dictionary with string keys and sets as values the task is to write a python program to concatenate keys all of which have similar values order irrespective.

Input : test_dict = {‘gfg’ : {5, 4, 3}, ‘is’ : {4, 3, 5}, ‘best’ : {1, 4, 3}, ‘for’ : {1, 3, 4}, ‘geeks’ : {1, 2, 3}}

Output : {‘gfg-is’: frozenset({3, 4, 5}), ‘best-for’: frozenset({1, 3, 4}), ‘geeks’: frozenset({1, 2, 3})}

Explanation : Similar set keys are concatenated, {5, 4, 3} == {4, 3, 5}, hence gfg-is are concatenated.

Input : test_dict = {‘gfg’ : {5, 4, 3}, ‘is’ : {4, 3, 5}, ‘geeks’ : {1, 2, 3}}

Output : {‘gfg-is’: frozenset({3, 4, 5}), ‘geeks’: frozenset({1, 2, 3})}

Explanation : Similar set keys are concatenated, {5, 4, 3} == {4, 3, 5}, hence gfg-is are concatenated.

Method #1 : Using defaultdict() + join() 

In this, we perform the task of hashing each set and appending corresponding keys using defaultdict(). The next step is to join all the hashed keys to similar set values. 

Python3




# Python3 code to demonstrate working of
# Concatenate similar set keys in Dictionary
# Using defaultdict() + join()
from collections import defaultdict
 
# initializing dictionary
test_dict = {'gfg': {5, 4, 3}, 'is': {4, 3, 5}, 'best': {
    1, 4, 3}, 'for': {1, 3, 4}, 'geeks': {1, 2, 3}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
hash_vals = defaultdict(list)
for key, val in test_dict.items():
 
    # values are hashed using frozenset
    hash_vals[frozenset(val)].append(key)
 
# perform joining
res = {'-'.join(keys): vals for (vals, keys) in hash_vals.items()}
 
# printing result
print("The concatenated keys : " + str(res))

Output:

The original dictionary is : {‘gfg’: {3, 4, 5}, ‘is’: {3, 4, 5}, ‘best’: {1, 3, 4}, ‘for’: {1, 3, 4}, ‘geeks’: {1, 2, 3}}

The concatenated keys : {‘gfg-is’: frozenset({3, 4, 5}), ‘best-for’: frozenset({1, 3, 4}), ‘geeks’: frozenset({1, 2, 3})}

Method #2 : Using groupby() + join() + dictionary comprehension

In this, we perform tasks of grouping like elements using groupby(). After that joining alike valued keys is done using join(). 

Python3




# Python3 code to demonstrate working of
# Concatenate similar set keys in Dictionary
# Using groupby() + join() + dictionary comprehension
from itertools import groupby
 
# initializing dictionary
test_dict = {'gfg': {5, 4, 3}, 'is': {4, 3, 5},
             'best': {1, 4, 3}, 'for': {1, 3, 4},
             'geeks': {1, 2, 3}}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# elements grouped using groupby()
# dictionary comprehension provides shorthand to perform grouping
res = {'-'.join(val): key for key, val in groupby(
  sorted(test_dict, key=test_dict.get), key=lambda sub: test_dict[sub])}
 
# printing result
print("The concatenated keys : " + str(res))

Output:

The original dictionary is : {‘gfg’: {3, 4, 5}, ‘is’: {3, 4, 5}, ‘best’: {1, 3, 4}, ‘for’: {1, 3, 4}, ‘geeks’: {1, 2, 3}}

The concatenated keys : {‘gfg-is’: {3, 4, 5}, ‘best-for’: {1, 3, 4}, ‘geeks’: {1, 2, 3}}


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!