0% found this document useful (0 votes)
5 views4 pages

Python Set Methods: Complete Reference

This document provides a comprehensive reference for Python's built-in set methods, detailing their syntax, arguments, and return types. It categorizes methods into those that modify the set in-place and those that perform mathematical operations, returning new sets or boolean values. Key methods include add, remove, union, intersection, and various update methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python Set Methods: Complete Reference

This document provides a comprehensive reference for Python's built-in set methods, detailing their syntax, arguments, and return types. It categorizes methods into those that modify the set in-place and those that perform mathematical operations, returning new sets or boolean values. Key methods include add, remove, union, intersection, and various update methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Built-in Set Methods Reference

This table provides a complete reference for all built-in methods available on Python's set
type. It details the method's syntax, its required and optional arguments, and its return type.

Set methods are divided into those that modify the set in-place and those that return a new
set (or a boolean result).

1. Set Modification Methods (In-Place Operations)


These methods directly change the contents of the set and always return None.

Method Arguments Description Return Type

add() (elem, /) Adds a single None


element to the set.
Does nothing if the
element is already
present.

remove() (elem, /) Removes the None


specified element.
Raises a KeyError if
the element is not
found.

discard() (elem, /) Removes the None


specified element if
it is present. Does
nothing if the
element is not
found (no error).

pop() () Removes and Type of Element


returns an arbitrary (varies)
element from the
set. Raises a
KeyError if the set
is empty.

clear() () Removes all None


elements from the
set, making it
empty.

update() (*others, /) Adds elements None


from all specified
iterable(s) to the
set. This is
equivalent to union
but modifies the
set in place.

intersection_update (*others, /) Updates the set, None


() keeping only
elements found in
both the set and all
specified
iterable(s).

difference_update() (*others, /) Updates the set, None


removing elements
found in any of the
specified
iterable(s).

symmetric_differen (other, /) Updates the set None


ce_update() with elements that
are in either the set
or the other
iterable, but not
both. (Only takes
one iterable
argument).

2. Mathematical and Comparison Methods


These methods perform standard set algebra operations, returning a new set or a boolean
value.

Method Arguments Description Return Type

union() (*others, /) Returns a new set set


containing all items
from the original
set and all
specified
iterable(s).

intersection() (*others, /) Returns a new set set


containing only
items present in
both the original
set and all
specified
iterable(s).

difference() (*others, /) Returns a new set set


with elements from
the original set that
are not found in
any of the
specified
iterable(s).

symmetric_differen (other, /) Returns a new set set


ce() with elements that
are in either the
original set or the
other iterable, but
not both.

issubset() (other, /) Returns True if the bool


set is contained
within the other
set/iterable.

issuperset() (other, /) Returns True if the bool


set contains the
other set/iterable.

isdisjoint() (other, /) Returns True if the bool


set has no
elements in
common with the
other set/iterable.
copy() () Returns a shallow set
copy of the set (a
new set object).

You might also like