Open In App

Python | Tuple key dictionary conversion

Improve
Improve
Like Article
Like
Save
Share
Report

Interconversions are always required while coding in Python, also because of expansion of Python as a prime language in the field of Data Science. This article discusses yet another problem that converts to dictionary and assigns keys as first pair elements as tuple and rest as it’s value. Let’s discuss certain ways in which this can be performed. 

Method #1: Using dictionary comprehension This problem can be solved using a shorthand made using dictionary comprehension which performs the classic Naive method of loops in single line inside a dictionary. 

Python3




# Python3 code to demonstrate
# Tuple key dictionary conversion
# using list comprehension
 
# initializing list
test_list = [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension
# Tuple key dictionary conversion
res = {(sub[0], sub[1]): sub[2:] for sub in test_list}
 
# print result
print("The dictionary after conversion : " + str(res))


Output : 

The original list : [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')]
The dictionary after conversion : {('Akash', 22): ('JIIT', ), ('Akshat', 22): ('JIIT', ), ('Nikhil', 21): ('JIIT', )}

Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(n), where n is the number of tuples in the list.

Method #2: Using dict() + dictionary comprehension Performs task similar to the above method, just the difference comes in the way of creation of dictionary. In the above method, dictionary is created using comprehension, here dict function is used for creation of a dictionary. 

Python3




# Python3 code to demonstrate
# Tuple key dictionary conversion
# using dict() + dictionary comprehension
 
# initializing list
test_list = [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')]
 
# printing original list
print("The original list : " + str(test_list))
 
# using dict() + dictionary comprehension
# Tuple key dictionary conversion
res = dict(((idx[0], idx[1]), idx[2:]) for idx in test_list)
 
# print result
print("The dictionary after conversion : " + str(res))


Output : 

The original list : [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')]
The dictionary after conversion : {('Akash', 22): ('JIIT', ), ('Akshat', 22): ('JIIT', ), ('Nikhil', 21): ('JIIT', )}

The time complexity of this code is O(n), where n is the length of the input list. 

The auxiliary space complexity of this code is O(n), where n is the length of the input list.

Method #3: Using the zip() function

First creates a list of keys by iterating over the tuples in the original list and selecting the first two elements of each tuple. Then, it creates a list of values by iterating over the tuples again and selecting everything after the first two elements of each tuple. Finally, it uses the zip() function to combine the keys and values lists into a single list of (key, value) tuples, and passes that list to the dict() function to create the resulting dictionary.

Python3




test_list = [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')]
 
# Create a list of keys by iterating over the test_list tuples and selecting the first two elements
keys = [(sub[0], sub[1]) for sub in test_list]
 
# Create a list of values by iterating over the test_list tuples and selecting everything after the first two elements
values = [sub[2:] for sub in test_list]
 
# Use the zip() function to combine the keys and values lists into a single list of (key, value) tuples
# Then, pass that list of tuples to the dict() function to create the dictionary
res = dict(zip(keys, values))
 
# Print the resulting dictionary
print(res)


Output

{('Nikhil', 21): ('JIIT',), ('Akash', 22): ('JIIT',), ('Akshat', 22): ('JIIT',)}

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list. 

Method #4: Using a for loop and the setdefault() method

  • A list of tuples test_list is initialized with three tuples 
  • An empty dictionary res is initialized to store the converted values.
  • A for loop is used to iterate through each tuple in the test_list.
  • In each iteration, a new key is created by extracting the first two elements of the tuple using (sub[0], sub[1]).
  • The remaining elements of the tuple are extracted into a value variable using slicing.
  • The setdefault() method is used to add the key to the dictionary with an empty list as its value if it doesn’t already exist. The method then returns the value associated with the key.
  • The extend() method is used to append the value to the list associated with the 

Python3




# initializing list
test_list = [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')]
 
# printing original list
print("The original list : " + str(test_list))
 
# using for loop and setdefault() method
res = {}
for sub in test_list:
    key = (sub[0], sub[1])
    value = sub[2:]
    res.setdefault(key, []).extend(value)
 
# print result
print("The dictionary after conversion : " + str(res))


Output

The original list : [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')]
The dictionary after conversion : {('Nikhil', 21): ['JIIT'], ('Akash', 22): ['JIIT'], ('Akshat', 22): ['JIIT']}

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list. 



Last Updated : 19 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads