Set (Python)

A set is an unordered collection of unique items written in curly braces. Sets automatically remove duplicates and are great for membership tests.

Python

Sets support fast math-style operations like union (|), intersection (&), and difference (-). Because they are unordered, you can't index into a set.

Syntax

colors = {"red", "green", "blue"}

Examples

Python

nums = {1, 2, 2, 3, 3, 3}
print(nums)
print(2 in nums)

Output:

{1, 2, 3}
True

Learn it in a lesson

See also

Back to the Dictionary