![]() |
VOOZH | about |
We are given a list containing lists, and our task is to convert each sublist into a set. This helps remove duplicate elements within each sublist. For example, if we have:
a = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]
The output should be:
[{1, 2}, {1, 2, 3}, {2}, {0}]
Let's explore some method's to achieve this.
This method uses a one-liner loop that converts each sublist into a set using the set() constructor inside a list comprehension.
[{1, 2}, {1, 2, 3}, {2}, {0}]
Explanation:
We use the map() function to apply set() to every sublist in the input list and then convert the result to a list.
[{1, 2}, {1, 2, 3}, {2}, {0}]
Explanation:
We use a traditional for loop to iterate over each sublist and append the converted set to a new list.
[{1, 2, 3}, {4, 5, 6}, {8, 9, 7}]Explanation: