Python | List of tuples to dictionary conversion
Last Updated :
24 Mar, 2023
Interconversions are always required while coding in Python, also because of the 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 1st element of 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
# List of tuple to 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
# List of tuple to dictionary conversion
res = {sub[0]: sub[1:] 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 : {'Nikhil': (21, 'JIIT'), 'Akshat': (22, 'JIIT'), 'Akash': (22, 'JIIT')}
Time complexity: O(n), where n is the number of tuples in the input list.
Auxiliary space: O(n), where n is the number of tuples in the input 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
# List of tuple to 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
# List of tuple to dictionary conversion
res = dict((idx[0], idx[1:]) 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 : {'Nikhil': (21, 'JIIT'), 'Akshat': (22, 'JIIT'), 'Akash': (22, 'JIIT')}
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. dict() + dictionary comprehension performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method 3- using a for loop:
This method iterates over each tuple in the list, extracts the first element (key) and the remaining elements (value) and assigns them to the dictionary.
Python3
test_list = [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')]
result_dict = {}
for tpl in test_list:
result_dict[tpl[0]] = tpl[1:]
print("The dictionary after conversion : " + str(result_dict))
OutputThe dictionary after conversion : {'Nikhil': (21, 'JIIT'), 'Akash': (22, 'JIIT'), 'Akshat': (22, 'JIIT')}
Time complexity: O(n), where n is the number of tuples in the list. The for loop iterates over each tuple once.
Auxiliary space: O(n), where n is the number of tuples in the list. The space required to store the dictionary is proportional to the number of tuples in the list.
Similar Reads
Python | Dictionary to list of tuple conversion Inter conversion between the datatypes is a problem that has many use cases and is usual subproblem in the bigger problem to solve. The conversion of tuple to dictionary has been discussed before. This article discusses a converse case in which one converts the dictionary to list of tuples as the wa
5 min read
Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read
Convert List of Dictionary to Tuple list Python Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co
5 min read
Python Convert Dictionary to List of Values Python has different types of built-in data structures to manage your data. A list is a collection of ordered items, whereas a dictionary is a key-value pair data. Both of them are unique in their own way. In this article, the dictionary is converted into a list of values in Python using various con
3 min read
Convert List of Named Tuples to Dictionary - Python We are given a list of named tuples we need to convert it into dictionary. For example, given a list li = [d("ojaswi"), d("priyank"), d("sireesha")], the goal is to convert it into a dictionary where each unique key maps to a list of its corresponding values, like {'Name': 'ojaswi'},{'Name': 'priyan
2 min read