Home / Python / Reference / Built-in functions

Python set() function

Make a new set: an unordered collection of unique items.

Syntax

set(iterable=())

Duplicates are removed. Use set() for an empty set; {} is an empty dictionary.

Parameters

iterableanything you can loop over

Return value

set

Example

Code

print(set([3, 1, 3, 2, 1]))
print(len(set("mississippi")))
print(sorted(set("mississippi")))
empty = set()
print(type(empty), type({}))

Output

{1, 2, 3}
4
['i', 'm', 'p', 's']
<class 'set'> <class 'dict'>

Related

Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.