Open In App

Python – Convert Lists of List to Dictionary

Last Updated : 05 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python records, we can have a problem in which we have data in the form of Lists of lists and we need to allocate certain elements as keys and certain as values to form a dictionary. This type of application can occur in data domains. Let’s discuss certain ways in which this task can be performed. 

Convert Lists of List to Dictionary in Python

Below are the ways by which we can convert lists of list to a Dictionary in Python:

  • Using Loop
  • Using Dictionary Comprehension
  • Using dictionary and tuple()
  • Using the zip() function and a loop
  • Using reduce() function

Convert Lists of List to Dictionary in Python Using Loop

This is the brute way in which we perform this task. In this, we iterate through the lists forming key value pairs according to require slicing. In this example, the Python code converts a list of lists (test_list) into a dictionary (res) using a loop, with the first two elements of each sublist as keys and the remaining elements as values.

Python3




# initializing list
test_list = [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert Lists of List to Dictionary
# Using loop
res = dict()
for sub in test_list:
    res[tuple(sub[:2])] = tuple(sub[2:])
 
# printing result
print("The mapped Dictionary : " + str(res))


Output

The original list is : [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}

Time Complexity: O(n) where n is the total number of values in the list “test_list”. 
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.

Python Convert Lists of List to Dictionary Using Dictionary Comprehension

In this example, the Python code converts a list of lists (test_list) into a dictionary (res) using dictionary comprehension, where the first two elements of each sublist serve as keys, and the remaining elements are used as values.

Python3




# initializing list
test_list = [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert Lists of List to Dictionary
# Using dictionary comprehension
res = {tuple(sub[:2]): tuple(sub[2:]) for sub in test_list}
 
# printing result
print("The mapped Dictionary : " + str(res))


Output

The original list is : [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}

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

Python Convert Nested List into Dictionary Using Dictionary and tuple()

We use a dictionary comprehension to create a dictionary with key-value pairs. The key of each pair is a tuple containing the first two elements of the sublists in the original list, and the value is a tuple containing the remaining elements of the sublists. We use the tuple() function to convert a sublist to a tuple.

Python3




original_list = [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
 
mapped_dict = {(lst[0], lst[1]): tuple(lst[2:]) for lst in original_list}
 
print("The mapped Dictionary :", mapped_dict)


Output

The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}

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

Convert Nested List into Dictionary Using the zip() Function and a Loop

In this example, the Python code converts a list of lists (test_list) into a dictionary (result_dict) using a combination of the zip() function and a loop. The first two elements of each sublist are used as keys, and the remaining elements are combined into tuples and used as values

Python3




# initializing list
test_list = [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert Lists of List to Dictionary
# Using zip() and loop
result_dict = {}
for sublist in test_list:
    key = tuple(sublist[:2])
    value = tuple(sublist[2:])
    result_dict[key] = value
 
# printing result
print("The mapped Dictionary : " + str(result_dict))


Output

The original list is : [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}

Time Complexity: O(nm), where n is the number of sublists in the list and m is the length of the sublists. 
Auxiliary Space: O(nm)

Convert Lists of List to Dictionary Using the reduce() Function

In this example, the Python code uses the functools.reduce() function to combine dictionaries generated from a list of lists (test_list). The combine_dicts function is defined to update and merge dictionaries, and the resulting combined dictionary (res) represents the conversion of the original list into a dictionary, where the first two elements of each sublist serve as keys, and the remaining elements are used as values.

Python3




from functools import reduce
 
# initializing list
test_list = [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
 
# printing original list
print("The original list is: " + str(test_list))
 
# define function to combine dictionaries
 
def combine_dicts(dict1, dict2):
    dict1.update(dict2)
    return dict1
 
# use reduce to apply combine_dicts to all nested dictionaries
res = reduce(combine_dicts, [
             {tuple(sub[:2]): tuple(sub[2:])} for sub in test_list])
 
# print mapped dictionary
print("The mapped dictionary: " + str(res))


Output

The original list is: [['a', 'b', 1, 2], ['c', 'd', 3, 4], ['e', 'f', 5, 6]]
The mapped dictionary: {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}

Time Complexity: O(n^2), where n is the length of the test_list.
Auxiliary Space: O(n), where n is the length of the test_list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads