Python Set difference()

Python’s set.difference(sets) method creates and returns a new set containing all elements of this set, except the ones in the given set argument or arguments. The resulting set has at most as many elements as this set. Here’s a minimal example where we return a new set with the elements from an existing set after … Read more

Python Set difference_update()

Python’s set.difference_update(*args) method removes all elements from this set that are members of any of the given set arguments. For example, s.difference_update({1, 2}) removes elements 1 and 2 from the set s. Its return value is None because it modifies the set it is called upon rather than creating a new set. Here’s a minimal … Read more

Python Set discard()

Python’s set.discard(x) method removes an element x from this set if it is a member. Otherwise does nothing. Here’s a minimal example where you remove the string element ‘Bob’ from the set by means of the s.discard() method: Syntax Let’s “dive” into the formal syntax of the set.discard() method. set.discard(element) Argument Data Type Explanation element … Read more

Python Set intersection()

Python’s set.intersection(sets) creates and returns a new set consisting of the elements that are members of all sets — this and the set argument(s). The resulting set has at most as many elements as any other set given in the argument list. Here’s a minimal example that creates a new set arising from the intersection … Read more

Python Set intersection_update()

Python’s set.intersection_update(sets) removes each element that’s not a member of all arguments sets. Here’s a minimal example that modifies existing set object s to be the intersection between sets s and t: Syntax Let’s dive into the formal syntax of the set.intersection_update() method. set.intersection_update(*sets) Argument Data Type Explanation *sets One or more sets The elements … Read more

Python Set isdisjoint()

Python’s set.isdisjoint(set) returns True if no element from this set is a member of the specified set. Sets are disjoint if and only if their intersection is the empty set. Here’s a minimal example that checks whether sets s and t are disjoint: Syntax Let’s dive into the formal syntax of the set.isdisjoint() method. set.isdisjoint(set) … Read more

Python Set issubset()

Python’s set.issubset(set) returns True if all elements of this set are members of the specified set argument. It determines whether the set on which the method is called is the subset of the specified set. Here’s a minimal example that checks whether sets s is a subset of t: Another minimal Harry Potter example: Syntax … Read more

Python Set issuperset()

Python’s set.issuperset(set) returns True if all elements of the specified set argument are members of this set. It determines whether the set on which the method is called is the superset of the specified set. Here’s a minimal example that checks whether set s is a superset of t: Another minimal Harry Potter example: Syntax … Read more

Python Set pop()

Python’s set.pop() method removes and returns an arbitrary element from the set. If no element is present — that is, you call it on an empty set — set.pop() raises a KeyError. Here’s a minimal example where you remove a string element that happens to be ‘Alice’ from the set by means of the s.pop() … Read more

Python Set symmetric_difference_update()

Python’s S.symmetric_difference_update(T) determines all elements that are in exactly one of the two sets S or T and updates set S with those elements. Here’s a minimal example where we update an existing set to consist of elements 1 and 4 that are in exactly one of the two sets s and t. Here’s another … Read more

Python Set add()

Python’s set.add(element) method adds the element to this set. If the element is already present in the set, the method returns without any side effect. Here’s a minimal example where we add element 4 to an existing set: Syntax Let’s dive into the formal syntax of the set.add() method. set.add(element) Argument Data Type Explanation element … Read more

Python Set union()

Python’s set.union(set_1, set_2, …) creates and returns a new set consisting of the elements that are members of any of the involved sets. The resulting set has at least as many elements as any other set given in the argument list. Here’s a minimal example that creates a new set arising from the union of … Read more