Skip to content

set

The built-in set data type represents an unordered collection of unique elements. Sets allow for efficient membership testing and set operations like union, intersection, and difference:

Language: Python
>>> fruits = {"apple", "banana", "cherry"}
>>> "banana" in fruits
True

set Constructor

Language: Python Syntax
set(iterable=(), /)

Arguments

Argument Description
iterable Optional positional-only iterable with elements to be included in the set. Defaults to an empty tuple, producing an empty set when omitted.

Return Value

  • Returns a Python set object

set Examples

Creating an empty instance of a set:

Language: Python
>>> empty_set = set()
>>> empty_set
set()

Creating instances using a literal:

Language: Python
>>> colors = {"red", "green", "blue"}
>>> colors
{'red', 'blue', 'green'}

Creating an instance using the class constructor with a list as an argument:

Language: Python
>>> numbers = set([1, 2, 3, 4, 5])
>>> numbers
{1, 2, 3, 4, 5}

Checking whether a value is in a set:

Language: Python
>>> "red" in colors
True

Adding or removing elements from a set (mutability):

Language: Python
>>> colors.add("yellow")
>>> colors
{'red', 'green', 'blue', 'yellow'}

>>> colors.remove("green")
>>> colors
{'red', 'blue', 'yellow'}

set Methods

Method Description
.add(elem) Adds an element to the set.
.remove(elem) Removes an element from the set; raises KeyError if not found.
.discard(elem) Removes an element from the set if present.
.pop() Removes and returns an arbitrary element; raises KeyError if empty.
.clear() Removes all elements from the set.
.copy() Returns a shallow copy of the set.
.update(*others) Updates the set with elements from all the others.
.intersection_update(*others) Updates the set, keeping only elements found in it and all others.
.difference_update(*others) Removes all elements found in others from this set.
.symmetric_difference_update(other) Updates the set with the symmetric difference.
.union(*others) Returns a new set with elements from the set and all others.
.intersection(*others) Returns a new set with elements common to the set and all others.
.difference(*others) Returns a new set with elements in the set that are not in the others.
.symmetric_difference(other) Returns a new set with elements in either the set or other but not both.
.issubset(other) Returns True if the set is a subset of other.
.issuperset(other) Returns True if the set is a superset of other.
.isdisjoint(other) Returns True if the set has no elements in common with other.

set Common Use Cases

The most common use cases for set include:

  • Removing duplicates from a sequence
  • Testing membership efficiently
  • Performing mathematical set operations, such as union, intersection, and difference
  • Storing unique items without any particular order

set Real-World Example

Consider this example of using a set to find unique visitors to a website:

Language: Python
>>> visitors = ["Alice", "Bob", "Alice", "Charlie", "Bob", "David"]
>>> unique_visitors = set(visitors)
>>> unique_visitors
{'Charlie', 'Bob', 'Alice', 'David'}

This example demonstrates how you can use a set to eliminate duplicate entries and efficiently identify unique elements.

Sets in Python

Tutorial

Sets in Python

Learn to work effectively with Python sets: define set objects, explore the supported operations, and know when sets fit your code.

basics python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Oct. 1, 2026 • Reviewed by Brenda Weleschuk and Dan Bader