set

Sets are mutable unordered collections of unique elements. Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.

Sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

Sets are implemented using dictionaries. They cannot contain mutable elements such as lists or dictionaries. However, they can contain immutable collections.

Constructors

set()
Returns a set type initialized from iterable.
{} set comprehension
Returns a set based on existing iterables.
literal syntax
Initializes a new instance of the set type.

Methods

Adding Elements

add
Adds a specified element to the set.
update
Adds specified elements to the set.

Deleting

discard
Removes an element from the set.
remove
Removes an element from the set (raises KeyError if not found).
pop
Removes and returns an arbitrary element from the set.
clear
Removes all elements from the set.

Information

issuperset
Returns a Boolean stating whether the set contains the specified set or iterable.
issubset
Returns a Boolean stating whether the set is contained in the specified set or iterable.
isdisjoint
Returns a Boolean stating whether the set contents do not overlap with the specified set or iterable.

Set Operations

difference
Returns a new set with elements in the set that are not in the specified iterables.
intersection
Returns a new set with elements common to the set and the specified iterables.
symmetric_difference
Returns a new set with elements in either the set or the specified iterable but not both.
union
Returns a new set with elements from the set and the specified iterables.

Set Operations Assignment

difference_update
Updates the set, removing elements found in the specified iterables.
intersection_update
Updates the set, keeping only elements found in it and the specified iterables.
symmetric_difference_update
Updates the set, keeping only elements found in either set or the specified iterable, but not in both.

Copying

copy
Returns a copy of the set.

Set Operators

Adding Elements

|= (update)
Adds elements from another set.

Relational Operators

== (is equal)
Returns a Boolean stating whether the set has the same elements as the other set.
!= (is not equal)
Returns a Boolean stating whether the set has different elements as the other set.
<= (issubset)
Returns a Boolean stating whether the set is contained in the other set.
< (issubset proper)
Returns a Boolean stating whether the set is contained in the specified set and that the sets are not equal.
>= (issuperset)
Returns a Boolean stating whether the set contains the other set.
> (issuperset proper)
Returns a Boolean stating whether the set contains the other set and that the sets are not equal.

Set Operations

- (difference)
Returns a new set with elements in the set that are not in the other set.
& (intersection)
Returns a new set with elements common to the set and the other set.
^ (symmetric_difference)
Returns a new set with elements in either the set or the other set but not both.
| (union)
Returns a new set with elements from the set and the other set.

Set Operations Assignment

-= (difference_update)
Updates the set, removing elements found in the other set.
&= (intersection_update)
Updates the set, keeping only elements found in it and the other set.
^= (symmetric_difference_update)
Updates the set, keeping only elements found in either set or the other set, but not in both.

Functions

len
Returns an int type specifying number of elements in the collection.
min
Returns the smallest item from a collection.
max
Returns the largest item in an iterable or the largest of two or more arguments.
sum
Returns a total of the items contained in the iterable object.
sorted
Returns a sorted list from the iterable.
reversed
Returns a reverse iterator over a sequence.
all
Returns a Boolean value that indicates whether the collection contains only values that evaluate to True.
any
Returns a Boolean value that indicates whether the collection contains any values that evaluate to True.
enumerate
Returns an enumerate object.
zip
Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.