set()
set([iterable]) Returns:
set · Updated March 13, 2026 · Built-in Functions built-in collections unique iteration
The set() function creates a new set object from an iterable. A set is an unordered collection of unique elements — duplicates are automatically removed. If no argument is provided, set() returns an empty set.
Syntax
set()
set(iterable)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
iterable | iterable | — | Optional. An iterable (list, tuple, string, dict, etc.) to convert to a set. If omitted, returns an empty set. |
Examples
Creating an empty set
# This is NOT an empty dictionary — it's an empty set
empty = set()
print(empty)
# set()
# Common mistake: don't use {} for empty sets
# {} creates an empty dict, not a set
empty_dict = {}
print(type(empty_dict))
# <class 'dict'>
Removing duplicates from a list
numbers = [1, 2, 2, 3, 3, 3, 4, 4, 5]
unique = set(numbers)
print(unique)
# {1, 2, 3, 4, 5}
# If you need a list back
unique_list = list(set(numbers))
print(unique_list)
# [1, 2, 3, 4, 5]
Converting from a string
# Characters are unique — duplicates removed, order not preserved
text = "hello world"
chars = set(text)
print(chars)
# {'h', ' ', 'l', 'o', 'w', 'r', 'd', 'e'}
# For alphabetical characters only
alpha = set("hello hello hello")
print(sorted(alpha))
# ['e', 'h', 'l', 'o']
Converting from a dictionary
# Only keys become set elements
data = {"a": 1, "b": 2, "c": 3}
keys = set(data)
print(keys)
# {'a', 'b', 'c'}
Common Patterns
Finding unique words in text
text = "the quick brown fox jumps over the lazy dog the fox"
words = set(text.lower().split())
print(words)
# {'the', 'quick', 'brown', 'fox', 'jumps', 'over', 'lazy', 'dog'}
print(f"Unique words: {len(words)}")
# Unique words: 8
Set operations with lists
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
# Intersection (common elements)
common = set(list1) & set(list2)
print(common)
# {4, 5}
# Union (all unique elements)
all_elements = set(list1) | set(list2)
print(all_elements)
# {1, 2, 3, 4, 5, 6, 7, 8}
# Difference (in list1 but not in list2)
diff = set(list1) - set(list2)
print(diff)
# {1, 2, 3}
Membership testing performance
# Sets have O(1) average lookup vs O(n) for lists
large_list = list(range(10000))
large_set = set(large_list)
# Both check membership the same way
print(5000 in large_list) # True
print(5000 in large_set) # True
# But set is much faster for large collections
import time
start = time.perf_counter()
_ = 9999 in large_list
list_time = time.perf_counter() - start
start = time.perf_counter()
_ = 9999 in large_set
set_time = time.perf_counter() - start
print(f"List: {list_time:.6f}s, Set: {set_time:.6f}s")
# List: ~0.0001s, Set: ~0.000001s
When to Use Sets
Use set() when you need:
- Unique elements (removing duplicates)
- Fast membership testing
- Mathematical set operations (intersection, union, difference)
Use a list instead when:
- Order matters
- You need indexed access
- You might have duplicate values intentionally
See Also
-
built-in::frozenset — immutable set variant
-
list::list — convert back to a list