Python – Concatenate all keys which have similar values
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 dictionarytest_dict = {'gfg': {5, 4, 3}, 'is': {4, 3, 5}, 'best': { 1, 4, 3}, 'for': {1, 3, 4}, 'geeks': {1, 2, 3}}# printing original dictionaryprint("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 joiningres = {'-'.join(keys): vals for (vals, keys) in hash_vals.items()}# printing resultprint("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 comprehensionfrom itertools import groupby# initializing dictionarytest_dict = {'gfg': {5, 4, 3}, 'is': {4, 3, 5}, 'best': {1, 4, 3}, 'for': {1, 3, 4}, 'geeks': {1, 2, 3}}# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# elements grouped using groupby()# dictionary comprehension provides shorthand to perform groupingres = {'-'.join(val): key for key, val in groupby( sorted(test_dict, key=test_dict.get), key=lambda sub: test_dict[sub])}# printing resultprint("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}}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


