set()

The deduplication tool, and the fastest membership test in Python. The price is that order is gone and elements must be hashable.

Built-in function / typePython 2.4+Live demo
Common call
set(iterable)
Returns
a new set — unique elements, arbitrary order
Replaces
a list plus an "if x not in seen" loop
Watch out
the empty set is set(), not {} — {} is an empty dict
set([iterable])
set

Demo

Live evaluation
Try:
Inputs
iterablestra string to split into unique characters
Output
set('hello')
{'e', 'h', 'l', 'o'}

set keeps one of each distinct element and discards the rest, so "hello" gives four characters rather than five — the second l is gone. The display order is arbitrary and carries no meaning; it reflects hash placement, not insertion. Note the empty case renders as set(), because {} already means an empty dict.

Parameters

NameTypeRequiredDescription
iterableiterableno (())Any iterable of hashable elements. Omitted gives an empty set. Unhashable elements raise TypeError.

Return value

setA new set containing the unique elements of iterable. With no argument, an empty set.

Common patterns

Deduplicate a sequence
The most common use by far. Wrap in sorted or list if you need an order back.
unique = set(items)
ordered = sorted(set(items))
Fast membership tests
O(1) instead of scanning a list — the win grows with size.
allowed = set(allowed_list)
if user_id in allowed:
    ...
Compare two collections
Set algebra says what changed far more clearly than nested loops.
added   = set(new) - set(old)
removed = set(old) - set(new)

Examples

1. Duplicates collapse
set('hello')
Returns
{'h', 'e', 'l', 'o'}
2. From a list
set([1, 2, 2, 3])
Returns
{1, 2, 3}
3. Empty
set()
Returns
set()
4. Braces make a dict
type({})
Returns
<class 'dict'>
5. Deduplicate + order
sorted(set([3, 1, 3]))
Returns
[1, 3]
6. Unhashable rejected
set([[1], [2]])
Returns
TypeError: unhashable type: 'list'

Pitfalls

1. The empty set is set(), not {}
Braces were taken by dict first, so {} is an empty dict. Writing {} for an empty set gives a mapping, and the failure surfaces later at an unrelated line.
Actually a dict
s = {}
type(s)
<class 'dict'>
Call the type
s = set()
type(s)
<class 'set'>
2. Order is not preserved and not stable
Sets have no order. The display order can differ between types, between runs for strings, and between Python versions. Never rely on it, and never index a set.
No indexing
set('abc')[0]
TypeError: 'set' object is not subscriptable
Sort for order
sorted(set('abc'))[0]
'a'
3. Elements must be hashable
Lists and dicts cannot go in a set. This bites when deduplicating rows — convert each row to a tuple first.
Lists rejected
set([[1, 2], [3]])
TypeError: unhashable type: 'list'
Tuples work
set([(1, 2), (3,)])
{(1, 2), (3,)}
4. True and 1 collapse into one element
Set membership uses equality, and True == 1, so a set cannot hold both. Whichever arrived first is the one kept.
Merged
set([1, True, 1.0])
{1}
Keep types apart
set([(int, 1), (bool, True)])
both survive

When to use

Use it
  • Removing duplicates from a sequence
  • Repeated membership tests against a fixed collection
  • Comparing collections — added, removed, shared
  • Any "have I seen this before" bookkeeping
Reach for something else
  • Order matters → a list, or dict.fromkeys to dedupe while keeping order
  • Elements are unhashable → convert to tuples first
  • You need the value as a dict key → frozenset, which is hashable

Notes

Complexity
O(n) to build; O(1) average for membership tests afterwards
Return
Always a new set; set(s) copies rather than returning s
CPython impl
Objects/setobject.c :: set_init
Memory
A hash table — noticeably larger per element than a list
Thread-safe
The construction is safe; the resulting set is not under concurrent mutation

FAQ

Use dict.fromkeys — dicts preserve insertion order since 3.7, so the keys come back in first-seen order. A set cannot do this because it has no order to preserve.

list(dict.fromkeys(items))

History

2.3
Sets arrived in the standard library as the sets module.
2.4
set and frozenset promoted to built-in types.
2.7
Set comprehensions and the {1, 2, 3} literal syntax added.