set.clear()

set.clear()
Returns: None · Updated March 15, 2026 · Set Methods
sets methods mutation

The .clear() method removes every element from a set, resulting in an empty set. This is a mutating operation — the original set object is modified in place.

Syntax

set.clear()

This method takes no parameters.

Examples

Basic usage

Remove all elements from a set:

colors = {"red", "green", "blue"}
colors.clear()
print(colors)
# set()  # Empty set

Clearing after processing

A common pattern is clearing a set after processing its contents:

queue = {1, 2, 3, 4, 5}

while queue:
    item = queue.pop()
    print(f"Processing: {item}")
    
print(f"Queue after processing: {queue}")
# Processing: 1
# Processing: 2
# Processing: 3
# Processing: 4
# Processing: 5
# Queue after processing: set()

Reinitializing vs clearing

Both approaches empty a set, but they behave differently with shared references:

original = {1, 2, 3}
reference = original

# Using clear() - both variables see the change
original.clear()
print(reference)  # set()

# Using reassignment - only the variable is affected
original = {1, 2, 3}
reference = original
original = set()  # New empty set
print(reference)  # {1, 2, 3}  # Original still intact

This demonstrates an important distinction: .clear() modifies the object in place, while reassignment creates a new object.

Use in data pipelines

Clear sets between processing batches:

def process_batch(batch):
    return [x * 2 for x in batch]

seen = set()
all_results = []

for batch in [[1, 2, 3], [2, 3, 4], [3, 4, 5]]:
    # Filter to only new items
    new_items = [x for x in batch if x not in seen]
    seen.update(batch)
    
    all_results.extend(process_batch(new_items))
    
    # Clear for next iteration (or keep if cumulative)
    # seen.clear()
    
print(all_results)
# [2, 3, 4, 6, 8, 8, 10, 12]

Common Patterns

Resetting a seen set

After completing a processing loop, reset a tracking set:

def find_unique_in_file(filepath):
    unique = set()
    with open(filepath) as f:
        for line in f:
            unique.add(line.strip())
    return unique

# After using the set
results = find_unique_in_file("data.txt")
print(len(results))

# Clear for reuse in next function call
results.clear()

Temporary storage

Use a set for temporary deduplication, then clear it:

temp_cache = set()

def add_to_cache(value):
    if value not in temp_cache:
        temp_cache.add(value)
        return True
    return False

# Reset cache when full
if len(temp_cache) > 1000:
    temp_cache.clear()
    print("Cache cleared")

Breaking circular references

If a set is part of a data structure with circular references, .clear() can help break the cycle before discarding:

class Node:
    def __init__(self, value):
        self.value = value
        self.connections = set()

a = Node("A")
b = Node("B")
a.connections.add(b)
b.connections.add(a)

# Before discarding
a.connections.clear()
b.connections.clear()

Performance

The .clear() method has:

  • Time complexity: O(n) where n is the number of elements
  • Space complexity: O(1) — the underlying hash table is reused

The operation must iterate through all elements to remove them, but it does not allocate new memory.

See Also