VOOZH about

URL: https://www.geeksforgeeks.org/python/python-concatenate-tuple-to-dictionary-key/

⇱ Python - Concatenate Tuple to Dictionary Key - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Concatenate Tuple to Dictionary Key

Last Updated : 28 Apr, 2023

Given Tuples, convert them to the dictionary with key being concatenated string.

Input : test_list = [(("gfg", "is", "best"), 10), (("gfg", "for", "cs"), 15)] 
Output : {'gfg is best': 10, 'gfg for cs': 15} 
Explanation : Tuple strings concatenated as strings. 
Input : test_list = [(("gfg", "is", "best"), 10)] 
Output : {'gfg is best': 10} 
Explanation : Tuple strings concatenated as strings.

Method #1 : Using loop + join()

In this, we perform the task of concatenation for the dictionary key using join() and loop is used to render the required dictionary.


Output
The original list is : [(('gfg', 'is', 'best'), 10), (('gfg', 'good'), 1), (('gfg', 'for', 'cs'), 15)]
The computed Dictionary : {'gfg is best': 10, 'gfg good': 1, 'gfg for cs': 15}

Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.

Method #2: Using dictionary comprehension

This is shorthand to the above method, similar functionality, just a one-liner on paper for a compact solution.


Output
The original list is : [(('gfg', 'is', 'best'), 10), (('gfg', 'good'), 1), (('gfg', 'for', 'cs'), 15)]
The computed Dictionary : {'gfg is best': 10, 'gfg good': 1, 'gfg for cs': 15}

Method #3: Using For loop and join() method.

Algorithm:

  1. Initialize an empty dictionary res.
  2. Traverse the list using a for loop and take each tuple in the list.
  3. Convert the first element of the tuple (which is a tuple itself) into a string by joining its elements using a space separator. Ignore any empty elements.
  4. Add an entry in the dictionary with the key as the string generated in step 3 and the value as the second element of the tuple.
  5. Print the resulting dictionary.

Output
The original list is : [(('gfg', 'is', 'best'), 10), (('gfg', 'good'), 1), (('gfg', 'for', 'cs'), 15)]
The computed Dictionary : {'gfg is best': 10, 'gfg good': 1, 'gfg for cs': 15}

Time complexity: O(n*k) where n is the length of the input list and k is the length of the longest tuple in the list.
Space complexity: O(n) as we are storing the result in a dictionary

Method #4: Using dictionary() and map()

Approach:

  1. Define a lambda function that takes a tuple and returns a tuple with the concatenated string as the first element and the second element as is.
  2. Use the map() function to apply the lambda function to each tuple in the list and create a new list of tuples.
  3. Use the dictionary() function to convert the list of tuples into a dictionary.

Below is the implementation of the above approach:


Output
The original list is : [(('gfg', 'is', 'best'), 10), (('gfg', 'good'), 1), (('gfg', 'for', 'cs'), 15)]
The computed Dictionary : {'gfg is best': 10, 'gfg good': 1, 'gfg for cs': 15}

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, for creating the new list of tuples.

Method #5: Using dict() constructor and zip()

  1. Initialize the original list with tuples containing a tuple of strings and an integer.
  2. Use the zip() function to pair up the concatenated string keys and the integer values of the tuples in the original list.
  3. Use the dict() constructor to create a dictionary from the pairs generated by zip().
  4. Print the resulting dictionary.

Output
The computed Dictionary : {'gfg is best': 10, 'gfg good': 1, 'gfg for cs': 15}

Time complexity: O(N), where n is the length of the original list.
Auxiliary space: O(N), where n is the length of the original list. This method creates two new lists, one for the concatenated string keys and one for the integer values, each with a length equal to the length of the original list.

Method #6: Using reduce() and lambda function from functools module


Output
The original list is : [(('gfg', 'is', 'best'), 10), (('gfg', 'good'), 1), (('gfg', 'for', 'cs'), 15)]
The computed Dictionary : {'gfg is best': 10, 'gfg good': 1, 'gfg for cs': 15}

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

Comment