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
test_list = [( 'Nikhil' , 21 , 'JIIT' ), ( 'Akash' , 22 , 'JIIT' ), ( 'Akshat' , 22 , 'JIIT' )]
print ("The original list : " + str (test_list))
res = {(sub[ 0 ], sub[ 1 ]): sub[ 2 :] for sub in test_list}
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
test_list = [( 'Nikhil' , 21 , 'JIIT' ), ( 'Akash' , 22 , 'JIIT' ), ( 'Akshat' , 22 , 'JIIT' )]
print ("The original list : " + str (test_list))
res = dict (((idx[ 0 ], idx[ 1 ]), idx[ 2 :]) for idx in test_list)
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' )]
keys = [(sub[ 0 ], sub[ 1 ]) for sub in test_list]
values = [sub[ 2 :] for sub in test_list]
res = dict ( zip (keys, values))
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
test_list = [( 'Nikhil' , 21 , 'JIIT' ), ( 'Akash' , 22 , 'JIIT' ), ( 'Akshat' , 22 , 'JIIT' )]
print ( "The original list : " + str (test_list))
res = {}
for sub in test_list:
key = (sub[ 0 ], sub[ 1 ])
value = sub[ 2 :]
res.setdefault(key, []).extend(value)
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Apr, 2023
Like Article
Save Article