Sunday, September 22, 2024
Google search engine
HomeLanguagesPython – Convert List of lists to list of Sets

Python – Convert List of lists to list of Sets

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}]

Method 1: Using list comprehension

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

Python3




# python3 program to convert list
# of lists to a list of sets
 
 
# 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:

he 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)

Method 2 : Using map() + 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




# Python3 code to demonstrate
# convert list of lists to list of sets
# using map() + set
 
# 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. The map() + set takes O(n) time
Auxiliary Space: O(n), extra space of size n is required 

Method 3: 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 

Method 4: Using a loop and 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), since we create a new set for each sublist in the input list.

RELATED ARTICLES

Most Popular

Recent Comments