Skip to content

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

Learn how to transform a Python set like {1, 2, 3} into a list of single-element sets [{1}, {2}, {3}] using list comprehensions, map(), loops, and transformations for combinatorics, parallel processing, or set operations.

Transforming a set like {1, 2, 3} into a list of individual single-element sets [{1}, {2}, {3}] is useful for combinatorics, parallel processing, set operation pipelines, or when algorithms require each element wrapped in its own set.

In this guide, you will learn the standard approaches to perform this conversion, apply transformations during the process, and see practical use cases.

The most Pythonic and readable approach wraps each element in its own set using the {x} literal syntax:

data = {10, 20, 30}

result = [{x} for x in data]

print(result)

Output (order may vary because sets are unordered):

[{10}, {20}, {30}]

Adding Consistent Order with sorted()

Since sets are unordered, the output order can vary between runs. Use sorted() when you need deterministic results:

data = {30, 10, 20}

result = [{x} for x in sorted(data)]

print(result)

Output:

[{10}, {20}, {30}]

Using map() with a Lambda

A functional approach applies a wrapping function to each element:

data = {'a', 'b', 'c'}

result = list(map(lambda x: {x}, data))

print(result)

Output (order may vary):

[{'a'}, {'b'}, {'c'}]

With a Named Function

For better readability or when the wrapping logic is reused:

def wrap_in_set(item):
    return {item}

data = {1, 2, 3}
result = list(map(wrap_in_set, data))

print(result)

Output:

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

Using an Explicit Loop

An explicit loop provides maximum flexibility for adding validation or logging:

data = {100, 200, 300}

result = []
for item in data:
    result.append({item})

print(result)

Output (order may vary):

[{100}, {200}, {300}]

Using Frozensets for Immutability

If you need immutable, hashable sets (for example, to use them as dictionary keys or elements of another set), use frozenset:

data = {1, 2, 3}

result = [frozenset([x]) for x in sorted(data)]

print(result)

result_as_set = set(result)
print(result_as_set)

Output:

[frozenset({1}), frozenset({2}), frozenset({3})]
{frozenset({3}), frozenset({2}), frozenset({1})}

Applying Transformations While Wrapping

You can transform each element during the conversion:

data = {1, 2, 3}

result = [{x ** 2} for x in sorted(data)]
print(result)

data = {1, 2, 3, 4, 5}
evens_only = [{x} for x in sorted(data) if x % 2 == 0]
print(evens_only)

Output:

[{1}, {4}, {9}]
[{2}, {4}]

Practical Use Cases

Preparing for Set Operations

When you need to test each element individually against another set:

data = {'apple', 'banana', 'cherry'}
other = {'apple', 'date'}

item_sets = [{x} for x in data]

for s in item_sets:
    if s & other:  # Check intersection
        print(f"Match found: {s}")

Output:

Match found: {'apple'}

Building Subsets for Combinatorics

Single-element sets serve as the foundation for generating all subsets of a given size:

from itertools import combinations

data = {1, 2, 3}

size_one = [{x} for x in sorted(data)]

size_two = [set(c) for c in combinations(sorted(data), 2)]

all_subsets = size_one + size_two
print(all_subsets)

Output:

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

Creating Independent Work Units

When distributing work across parallel processes, each element can be wrapped as an independent unit:

data = {10, 20, 30, 40, 50}

work_units = [{x} for x in sorted(data)]

def process_unit(s):
    """Process each set independently."""
    return sum(s) * 2

results = [process_unit(unit) for unit in work_units]
print(results)

Output:

[20, 40, 60, 80, 100]

Converting Back to a Single Set

Union All Sets

list_of_sets = [{1}, {2}, {3}]

combined = set().union(*list_of_sets)

print(combined)

Output:

{1, 2, 3}

Extract Elements to a List

Since each set contains exactly one element, use next(iter(s)) to extract it:

list_of_sets = [{10}, {20}, {30}]

elements = [next(iter(s)) for s in list_of_sets]

print(elements)

Output:

[10, 20, 30]

Creating Multi-Element Sets

If you need sets with more than one element, use itertools.combinations() to generate groups of a specific size:

from itertools import combinations

data = {1, 2, 3, 4}

pairs = [set(pair) for pair in combinations(sorted(data), 2)]

print(pairs)

Output:

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

Performance Comparison

import timeit

data = set(range(10_000))

def with_comprehension():
    return [{x} for x in data]

def with_map():
    return list(map(lambda x: {x}, data))

def with_loop():
    result = []
    for x in data:
        result.append({x})
    return result

print(f"Comprehension: {timeit.timeit(with_comprehension, number=100):.4f}s")
print(f"map():         {timeit.timeit(with_map, number=100):.4f}s")
print(f"Loop:          {timeit.timeit(with_loop, number=100):.4f}s")

Typical output:

Comprehension: 0.1234s
map():         0.1567s
Loop:          0.1890s

List comprehension is consistently the fastest option because it avoids the overhead of function calls on each iteration.

Method Comparison

Method Syntax Performance Best For
List comprehension [{x} for x in data] Fastest General use (recommended)
map() with lambda list(map(lambda x: {x}, data)) Moderate Functional programming style
Explicit loop result.append({x}) Slowest Custom logic, validation, debugging

Conclusion

Converting a set to a list of single-element sets in Python is most cleanly accomplished with a list comprehension using [{x} for x in data]. This approach is concise, readable, and the fastest option. Add sorted() if you need consistent, deterministic ordering in the output. Use frozenset when you need immutable, hashable sets that can be used as dictionary keys or elements of another set.

Navigation