Python Set Methods

Python set class provides many methods that transform or operate on the items of the given set.

In this tutorial, we will go through the methods of a Python Set with examples.

The following are the individual tutorials for each of the set class methods.

  • Python set.add() Python Tutorial for set add() method to add an element to the set.
  • Python set.clear() Python Tutorial for set clear() method to remove all the elements from the set.
  • Python set.copy() Python Tutorial for set copy() method to make a shallow copy of the set.
  • Python set.difference() Python Tutorial for set difference() method to find the set of elements are present in this set, but not in other set.
  • Python set.difference_update() Python Tutorial for set difference_update() method to remove the items from this set that are present in other sets.
  • Python set.discard() Python Tutorial for set discard() method to remove the specified element from the set.
  • Python set.intersection() Python Tutorial for set intersection() method to find the intersection of given two sets.
  • Python set.intersection_update() Python Tutorial for set intersection_update() method to remove the items from this set that are not present in the other set(s).
  • Python set.isdisjoint() Python Tutorial for set isdisjoint() method to check if two sets are disjoint (have no common elements).
  • Python set.issubset() Python Tutorial for set issubset() method to check if other set contains all the elements of this set or not.
  • Python set.issuperset() Python Tutorial for set issuperset() method to check if all the elements of other set are present in this set or not.
  • Python set.pop() Python Tutorial for set pop() method to remove an element from the set.
  • Python set.remove() Python Tutorial for set remove() method to remove specified element from the set.
  • Python set.symmetric_difference() Python Tutorial for set symmetric_difference() method to find the symmetric differences of given two sets.
  • Python set.symmetric_difference_update() Python Tutorial for set symmetric_difference_update() method to insert the symmetric differences from this set and the other set.
  • Python set.union() Python Tutorial for set union() method to find the union of sets.
  • Python set.update() Python Tutorial for set update() method to update this set with the union of this set and other sets.

Conclusion

In this Python Tutorial, we learned about Python Set Methods and their usage with explanation and example programs.