VOOZH about

URL: https://www.geeksforgeeks.org/python/python-concatenate-all-keys-which-have-similar-values/

⇱ Python - Concatenate all keys which have similar values - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Concatenate all keys which have similar values

Last Updated : 23 Jul, 2025

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. 

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})}

Time Complexity: O(n)
Auxiliary Space: O(n)

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(). 

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}}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Using normal dictionary iteration and list comprehension

we will iterate through the original dictionary and create a new dictionary using list comprehension to concatenate the keys with the same values.

Step-by-step approach:

  • Create an empty dictionary to store the concatenated keys and values.
  • Create an empty set to keep track of the processed keys.
  • Loop through each key-value pair in the dictionary.
  • Check if the current key has already been processed. If it has, skip it and move to the next key.
  • Create a set to store the keys that have the same set of values as the current key. Add the current key to this set.
  • Loop through the remaining keys in the dictionary.
  • Check if the current key has already been processed or is the same as the previous key. If it has, skip it and move to the next key.
  • Check if the values of the current key and the previous key are the same. If they are, add the current key to the set of similar keys.
  • Concatenate the keys in the set of similar keys and store the result in the dictionary, using the sorted keys as the concatenated key.
  • Add all the keys in the set of similar keys and the current key to the set of processed keys.
  • Print the concatenated keys and values in the resulting dictionary.

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}}

Time complexity: O(n^2), where n is the number of key-value pairs in the dictionary. This is because we need to compare each pair of keys with each other.

Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary. This is because we need to create a set to store the processed keys, which can have up to n elements.

Method #4: Using the heapq module:

Algorithm:

  1. Create an empty dictionary res to store the final result.
  2. Convert the original dictionary test_dict into a list of tuples (key, value) and then create another list heap of tuples where the second element is the key and the first element is the negative of the length of the corresponding value set.
  3. Heapify the list heap.
  4. Repeat the following steps until heap is not empty:
    a. Pop an element from the heap as (_, key, val).
    b. Check if any remaining keys have the same value as this key, if yes, append them to the current key by joining with '-'.
    c. Add the current key and val pair to the res dictionary.
  5. Print the res dictionary.

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 : {'best-for': {1, 3, 4}, 'geeks': {1, 2, 3}, 'gfg-is': {3, 4, 5}}

Time Complexity: The time complexity of the solution is O(n log n) where n is the number of elements in the input dictionary. This is because heapifying the list of tuples takes O(n log n) time and popping elements from the heap takes O(log n) time for each element. The while loop inside the outer loop may execute multiple times, but the total number of iterations across all iterations is still bounded by n.


Auxiliary Space: The space complexity of the solution is O(n) where n is the number of elements in the input dictionary. This is because we create a heap of n tuples, each containing the key and the negative of the length of the value set. The dictionary res also has n entries.

Comment