Set Methods

What You'll Learn: In this tutorial, you'll discover various built-in methods that you can use on sets in Python. These methods help you manage and manipulate set items efficiently.

Set Methods and Descriptions

  1. add()
    • Description: Adds an element to the set.
    • Example Code:

      python
      myset = {"apple", "banana"}
      myset.add("cherry")
      print(myset)
      
  2. clear()
    • Description: Removes all the elements from the set.
    • Example Code:

      python
      myset = {"apple", "banana", "cherry"}
      myset.clear()
      print(myset)
      
  3. copy()
    • Description: Returns a copy of the set.
    • Example Code:

      python
      myset = {"apple", "banana", "cherry"}
      newset = myset.copy()
      print(newset)
      
  4. difference()
    • Description: Returns a set containing the difference between two or more sets.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"banana", "cherry", "date"}
      result = set1.difference(set2)
      print(result)
      
  5. difference_update()
    • Description: Removes the items in this set that are also included in another, specified set.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"banana", "cherry", "date"}
      set1.difference_update(set2)
      print(set1)
      
  6. discard()
    • Description: Removes the specified item.
    • Example Code:

      python
      myset = {"apple", "banana", "cherry"}
      myset.discard("banana")
      print(myset)
      
  7. intersection()
    • Description: Returns a set that is the intersection of two or more sets.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"banana", "cherry", "date"}
      result = set1.intersection(set2)
      print(result)
      
  8. intersection_update()
    • Description: Removes the items in this set that are not present in other, specified sets.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"banana", "cherry", "date"}
      set1.intersection_update(set2)
      print(set1)
      
  9. isdisjoint()
    • Description: Returns whether two sets have an intersection or not.
    • Example Code:

      python
      set1 = {"apple", "banana"}
      set2 = {"cherry", "date"}
      result = set1.isdisjoint(set2)
      print(result)
      
  10. issubset()
    • Description: Returns whether another set contains this set or not.
    • Example Code:

      python
      set1 = {"apple", "banana"}
      set2 = {"apple", "banana", "cherry"}
      result = set1.issubset(set2)
      print(result)
      
  11. issuperset()
    • Description: Returns whether this set contains another set or not.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"apple", "banana"}
      result = set1.issuperset(set2)
      print(result)
      
  12. pop()
    • Description: Removes an element from the set.
    • Example Code:

      python
      myset = {"apple", "banana", "cherry"}
      myset.pop()
      print(myset)
      
  13. remove()
    • Description: Removes the specified element.
    • Example Code:

      python
      myset = {"apple", "banana", "cherry"}
      myset.remove("banana")
      print(myset)
      
  14. symmetric_difference()
    • Description: Returns a set with the symmetric differences of two sets.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"banana", "cherry", "date"}
      result = set1.symmetric_difference(set2)
      print(result)
      
  15. symmetric_difference_update()
    • Description: Inserts the symmetric differences from this set and another.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"banana", "cherry", "date"}
      set1.symmetric_difference_update(set2)
      print(set1)
      
  16. union()
    • Description: Returns a set containing the union of sets.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"banana", "cherry", "date"}
      result = set1.union(set2)
      print(result)
      
  17. update()
    • Description: Updates the set with the union of this set and others.
    • Example Code:

      python
      set1 = {"apple", "banana", "cherry"}
      set2 = {"banana", "cherry", "date"}
      set1.update(set2)
      print(set1)
      

Try It Yourself: Fun Exercises

  1. Add New Fruits:
    • Create a set of fruits.
    • Use the add() method to add a new fruit.
  2. Compare Your Favorite Foods:
    • Make two sets of your favorite foods and your friend's favorite foods.
    • Use the intersection() method to find common foods.
  3. Combine Hobbies:
    • Write sets of hobbies for you and your sibling.
    • Use the union() method to combine them into one set.

Summary:

In this Python tutorial, we learned about various set methods including add(), clear(), copy(), difference(), intersection(), union(), and more. These methods help you manage and manipulate sets effectively. Keep experimenting and have fun with sets in Python!