Open In App

Python – Convert List of lists to list of Sets

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list containing lists, the task is to write a Python Program to convert it to a list containing sets.

Examples:

Input : [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]
Output : [{1, 2}, {1, 2, 3}, {2}, {0}]

Input : [[4, 4], [5, 5, 5], [1, 2, 3]]
Output : [{4}, {5}, {1, 2, 3}]

Python Convert List of lists to list of Sets

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

Converting List of Lists to List of Sets Using List Comprehension

This can easily be achieved using list comprehension. We just iterate through each list converting the lists to the sets.

Python3




# initializing list
test_list = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]
 
# printing original list
print("The original list of lists : " + str(test_list))
 
# using list comprehension
# convert list of lists to list of sets
res = [set(ele) for ele in test_list]
 
# print result
print("The converted list of sets : " + str(res))


Output

The original list of lists : [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]
The converted list of sets : [{1, 2}, {1, 2, 3}, {2}, {0}]

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

Python Convert List of lists to list of Sets Using map() and set

We can use the combination of map function and set operator to perform this particular task. The map function binds each list and converts it into the set.

Python3




# initializing list
test_list = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]
 
# printing original list
print("The original list of lists : " + str(test_list))
 
# using map() + set
res = list(map(set, test_list))
 
# print result
print("The converted list of sets : " + str(res))


Output

The original list of lists : [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]
The converted list of sets : [{1, 2}, {1, 2, 3}, {2}, {0}]

Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), extra space of size n is required 

Convert List of lists to list of Sets Using a loop and a set constructor

In this method, it creates a list of lists lst_of_lsts and then converts each sublist to a set using a for loop. The resulting sets are stored in a new list called lst_of_sets, and then the contents of the lst_of_sets are printed to the console.

Python3




lst_of_lsts = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
lst_of_sets = []
 
for lst in lst_of_lsts:
    lst_of_sets.append(set(lst))
 
print(lst_of_sets)


Output

[{1, 2, 3}, {4, 5, 6}, {8, 9, 7}]

Time complexity: O(n), where n is the length of the lst_of_lsts.
Auxiliary Space: O(n), extra space of size n is required 

Convert List of lists to list of Sets Using a list constructor

This method iterates through each element of the input list test_list using a for loop. For each element, it creates a set using the set() constructor and appends it to a result list res using the append() method. Finally, it prints the result.

Python3




# initializing list
test_list = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]
 
# printing original list
print("The original list of lists : " + str(test_list))
 
# using a loop and a list constructor
# convert list of lists to list of sets
res = []
for ele in test_list:
    res.append(set(ele))
 
# print result
print("The converted list of sets : " + str(res))


Output

The original list of lists : [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]
The converted list of sets : [{1, 2}, {1, 2, 3}, {2}, {0}]

Time complexity: O(nm), where n is the length of the input list and m is the maximum length of any sublist in the input list. 
Auxiliary space: O(nm)



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