Skip to content

How to Convert a List of Lists to a List of Sets in Python

Learn how to remove duplicates from nested lists in Python by converting each sublist to a set, including handling unhashable elements and preserving order when needed.

Converting a list of lists into a list of sets is a common operation when you need to remove duplicate elements within each sublist or leverage set operations like intersections, unions, and differences. Sets automatically discard duplicates and provide efficient membership testing, making this conversion useful for data cleaning, deduplication, and mathematical computations.

In this guide, you will learn multiple methods to convert each sublist into a set, handle edge cases like unhashable elements, and choose the right approach for your situation.

Understanding the Problem

Given a list where each element is itself a list that may contain duplicates:

a = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]

The goal is to convert each sublist into a set, removing duplicates within each one:

[{1, 2}, {1, 2, 3}, {2}, {0}]

The most Pythonic approach uses a list comprehension with the set() constructor:

a = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]

result = [set(sublist) for sublist in a]

print(result)

Output:

[{1, 2}, {1, 2, 3}, {2}, {0}]

How it works:

  1. The list comprehension iterates over each sublist in a
  2. set(sublist) converts each sublist into a set, automatically removing duplicate elements
  3. The results are collected into a new list

Method 2: Using map() with set

The map() function applies set to every element of the input list in a single call:

a = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]

result = list(map(set, a))

print(result)

Output:

[{1, 2}, {1, 2, 3}, {2}, {0}]

map(set, a) returns a lazy iterator, so wrapping it with list() materializes the result. This approach is functionally equivalent to the list comprehension but uses a more functional programming style.

Method 3: Using a for Loop

An explicit loop provides maximum clarity and flexibility for adding custom logic during conversion:

a = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]

result = []
for sublist in a:
    result.append(set(sublist))

print(result)

Output:

[{1, 2}, {1, 2, 3}, {2}, {0}]

This approach is ideal when you need validation, filtering, or logging during the conversion:

a = [[1, 2, 1], [1, 2, 3], [], [2, 2, 2, 2], [0]]

result = []
for sublist in a:
    if sublist:  # Skip empty sublists
        converted = set(sublist)
        removed = len(sublist) - len(converted)
        print(f"  {sublist} -> {converted} (removed {removed} duplicate(s))")
        result.append(converted)

print(f"\nResult: {result}")

Output:

  [1, 2, 1] -> {1, 2} (removed 1 duplicate(s))
  [1, 2, 3] -> {1, 2, 3} (removed 0 duplicate(s))
  [2, 2, 2, 2] -> {2} (removed 3 duplicate(s))
  [0] -> {0} (removed 0 duplicate(s))

Result: [{1, 2}, {1, 2, 3}, {2}, {0}]

Working with Strings

The same techniques work seamlessly with lists of string lists:

a = [["apple", "banana", "apple"], ["cat", "dog", "cat", "dog"], ["hello"]]

result = [set(sublist) for sublist in a]

print(result)

Output:

[{'apple', 'banana'}, {'dog', 'cat'}, {'hello'}]

Common Mistake: Unhashable Elements in Sublists

Sets require all elements to be hashable. If your sublists contain unhashable types like lists or dictionaries, the conversion will fail.

Incorrect input (sublists contain lists, which are unhashable):

a = [[1, [2, 3]], [4, [5, 6]]]

result = [set(sublist) for sublist in a]

Error:

TypeError: unhashable type: 'list'

Fix by converting inner lists to tuples first (tuples are hashable):

a = [[1, [2, 3]], [4, [5, 6]]]

result = [
    set(tuple(item) if isinstance(item, list) else item for item in sublist)
    for sublist in a
]

print(result)

Output:

[{(2, 3), 1}, {4, (5, 6)}]

Converting Back: List of Sets to List of Lists

If you later need to convert the sets back to lists, for example for JSON serialization which does not support sets, use a list comprehension with sorted() for consistent ordering:

sets = [{1, 2}, {1, 2, 3}, {2}, {0}]

lists = [sorted(s) for s in sets]

print(lists)

Output:

[[1, 2], [1, 2, 3], [2], [0]]

Preserving Order While Removing Duplicates

If you need to remove duplicates but preserve the original order of elements, which sets do not guarantee, use dict.fromkeys() instead of set():

a = [[3, 1, 2, 1, 3], [5, 4, 5, 4, 3]]

sets_result = [set(sublist) for sublist in a]
print(f"Sets (unordered):        {sets_result}")

ordered_result = [list(dict.fromkeys(sublist)) for sublist in a]
print(f"Ordered (deduplicated):  {ordered_result}")

Output:

Sets (unordered):        [{1, 2, 3}, {3, 4, 5}]
Ordered (deduplicated):  [[3, 1, 2], [5, 4, 3]]

The dict.fromkeys() approach keeps elements in the order they first appear, which is important when the sequence carries meaning.

Leveraging Set Operations After Conversion

One of the main benefits of converting to sets is the ability to perform efficient set operations:

a = [[1, 2, 3, 2], [2, 3, 4, 3], [3, 4, 5, 4]]

sets = [set(sublist) for sublist in a]

common = sets[0].intersection(*sets[1:])
print(f"Common to all: {common}")

all_elements = sets[0].union(*sets[1:])
print(f"All unique:    {all_elements}")

only_first = sets[0] - sets[1] - sets[2]
print(f"Only in first: {only_first}")

Output:

Common to all: {3}
All unique:    {1, 2, 3, 4, 5}
Only in first: {1}

Method Comparison

Method Readability Flexibility Performance Best For
List comprehension High Moderate Fastest General use (recommended)
map() + set High Low Fast Simple, functional style
for loop Highest High Slightly slower Validation, logging, complex logic

Conclusion

Converting a list of lists to a list of sets in Python is straightforward with multiple approaches available:

  • Use a list comprehension ([set(x) for x in a]) for the cleanest, most Pythonic solution. This is the recommended approach for most cases.
  • Use map(set, a) for a concise functional approach when no additional transformation or filtering is needed.
  • Use a for loop when you need validation, filtering, error handling, or logging during conversion.

Keep in mind that sets automatically remove duplicates but are unordered. If you need deduplication while preserving the original element order, use dict.fromkeys() instead. And always ensure that all elements in your sublists are hashable before converting to sets, converting inner lists to tuples if necessary.

Navigation