Python Sets

Python Set update() Method

Updates a set, adding the unique items in the other set or iterable to the source set.

Python Set symmetric_difference_update() Method

Removes the items from the given set that are present in both sets, and inserts the items that are not present in both sets.

Python Sets

Sets allow multiple values to be stored as a collection.

Python Set add() Method

Adds an item to a set.

Python Set union() Method

Returns the union of two or more sets.

Python Set clear() Method

Removes all the items from the set, returning an empty set.

Python Set difference() Method

Returns a set containing the differences between two sets. If you have two sets, a and b, a.difference(b) returns all the items in a that are not in b.

Python Set difference_update() Method

Updates the source set by removing the items that exist in both sets.

Python Set discard() Method

Removes the specified item from the set. If the item does not exist, no exception is thrown. Compare this to the remove method which will throw an exception if the item doesn't exist.

Python Set intersection() Method

Returns a new set with items that all sets include.

Python Set intersection_update() Method

Removes the items in the sets involved in the operation so that only items present in all sets will remain in each set.

Python Set isdisjoint() Method

Returns True if the two sets do not have any items in common. Returns False if the two sets have an item in common (the two sets have an intersection).

Python Set issubset() Method

Returns True if the source set is contained in the other set.

Python Set issuperset() Method

Returns True if all the items in the other set exist in the source set.

Python Set pop() Method

Removes a random element from the set.

Python Set remove() Method

Removes an item from the set. If the item does not exist in the set, Python will throw a KeyError exception. Compare this to the discard method which will not throw an exception if the item doesn't exist.

Python Set symmetric_difference() Method

Returns a new set that contains all the items from both sets except items that are present in both sets.

Python Set copy() Method

Returns a copy of the set. The original set and the new set are now independent of each other.