5 Best Ways to Convert List of Lists to List of Sets in Python

πŸ’‘ Problem Formulation: In Python, it’s common to encounter a scenario where you have a list of lists, and you aim to convert each of these lists into sets. The objective is to perform this operation to facilitate set operations like union, intersection, and set difference, which are not directly available for lists. For example, the input [["apple", "banana"], ["banana", "cherry"], ["apple"]] should be converted to the output [{"apple", "banana"}, {"banana", "cherry"}, {"apple"}].

Method 1: Using a for-loop

One of the most fundamental methods to convert a list of lists to a list of sets is by iterating through each list using a for-loop and converting it to a set. This approach is simple and easy to understand, but may not be the most Pythonic or efficient with large datasets.

Here’s an example:

input_list = [["apple", "banana"], ["banana", "cherry"], ["apple"]]
list_of_sets = []
for sublist in input_list:
    list_of_sets.append(set(sublist))

Output:

[{'banana', 'apple'}, {'cherry', 'banana'}, {'apple'}]

This code snippet creates an empty list called list_of_sets and then for each sublist in our input list, it converts the sublist into a set and appends it to the list_of_sets. The result is a new list where each element is a set derived from the corresponding sublist.

Method 2: Using list comprehension

List comprehension in Python provides a concise way to create lists. It consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. This method is more Pythonic and can be more readable than a for-loop for those familiar with list comprehensions.

Here’s an example:

input_list = [["apple", "banana"], ["banana", "cherry"], ["apple"]]
list_of_sets = [set(sublist) for sublist in input_list]

Output:

[{'apple', 'banana'}, {'banana', 'cherry'}, {'apple'}]

In this code snippet, we use a list comprehension to iterate over all sublists in input_list and apply the set() function to each one. The result is a new list with the same number of sets as there were sublists.

Method 3: Using the map function

The map function in Python applies a specified function to each item of an iterable (such as a list) and returns a list of the results. It can be used for converting each sublist in a list of lists to a set in a clean and functional way.

Here’s an example:

input_list = [["apple", "banana"], ["banana", "cherry"], ["apple"]]
list_of_sets = list(map(set, input_list))

Output:

[{'banana', 'apple'}, {'cherry', 'banana'}, {'apple'}]

This code snippet leverages the map() function to convert each sublist within input_list into a set. This is done by passing the set constructor as the first argument to map(), and input_list as the second. The result is then converted to a list, creating our desired list of sets.

Method 4: Using generator expressions

Generator expressions are similar to list comprehensions but instead of creating a list and storing all the elements at once, they generate each element on the fly and are more memory-efficient. This method is suitable for large datasets where memory consumption is a concern.

Here’s an example:

input_list = [["apple", "banana"], ["banana", "cherry"], ["apple"]]
list_of_sets = list(set(sublist) for sublist in input_list)

Output:

[{'apple', 'banana'}, {'banana', 'cherry'}, {'apple'}]

Here, we utilize a generator expression ((set(sublist) for sublist in input_list)) which is very similar to the list comprehension used in Method 2, but uses parentheses instead of square brackets. The list() constructor then converts it to a list, giving us the necessary list of sets.

Bonus One-Liner Method 5: Using a nested set comprehension

Python supports the concept of a set comprehension, which is similar to list comprehensions. It provides a compact way to apply operations to elements, resulting in a set. This method is for those who like concise, one-liner solutions.

Here’s an example:

input_list = [["apple", "banana"], ["banana", "cherry"], ["apple"]]
list_of_sets = [{item for item in sublist} for sublist in input_list]

Output:

[{'apple', 'banana'}, {'cherry', 'banana'}, {'apple'}]

This code snippet is a one-liner that features a nested set comprehension inside a list comprehension. It iterates through each element in each sublist, creating a set for each iteration, which is then collected into a list, achieving the desired result of a list of sets in a concise manner.

Summary/Discussion

  • Method 1: For-loop. It’s clear and easy to understand for beginners. However, it can be verbose and is not the best in terms of performance when dealing with very large lists.
  • Method 2: List comprehension. Provides a cleaner and more Pythonic way of achieving the result but may be less intuitive for those new to Python.
  • Method 3: Map function. Offers a functional programming approach that is both concise and expressive. It might be less intuitive to read for those not accustomed to functional programming.
  • Method 4: Generator expression. The most memory-efficient method which is useful for large datasets, but makes code slightly less readable due to the additional complexity.
  • Method 5: Nested set comprehension. Provides a very concise way to achieve the desired outcome and is elegant, but may be confusing to read for those not familiar with comprehension syntax.