Add, delete, intersection, union, difference in Python set, Add elements to the set in Python, Remove element from set in Python, Intersection, union, and difference operations in Python set

Add, delete, intersection, union, difference in Python set

In this tutorial, we'll demonstrate how to perform add, delete, intersection, union, and difference operations on Python sets.

Add

Use the add() method to add an element to a set:

my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4} 

Delete

Use the remove() or discard() method to delete an element from a set:

# Using remove() my_set = {1, 2, 3} my_set.remove(1) print(my_set) # Output: {2, 3} # Using discard() my_set = {1, 2, 3} my_set.discard(1) print(my_set) # Output: {2, 3} 

Intersection

Use the intersection() method or the & operator to find the intersection of two sets:

set_a = {1, 2, 3} set_b = {3, 4, 5} # Using intersection() method intersection_set = set_a.intersection(set_b) print(intersection_set) # Output: {3} # Using & operator intersection_set = set_a & set_b print(intersection_set) # Output: {3} 

Union

Use the union() method or the | operator to find the union of two sets:

set_a = {1, 2, 3} set_b = {3, 4, 5} # Using union() method union_set = set_a.union(set_b) print(union_set) # Output: {1, 2, 3, 4, 5} # Using | operator union_set = set_a | set_b print(union_set) # Output: {1, 2, 3, 4, 5} 

Difference

Use the difference() method or the - operator to find the difference of two sets:

set_a = {1, 2, 3} set_b = {3, 4, 5} # Using difference() method difference_set = set_a.difference(set_b) print(difference_set) # Output: {1, 2} # Using - operator difference_set = set_a - set_b print(difference_set) # Output: {1, 2} 

Examples

  1. How to Add Elements to a Python Set:

    • Use the add() method to add a single element, and update() to add multiple elements.
    # Example my_set = {1, 2, 3} my_set.add(4) # Adds a single element my_set.update({5, 6}) # Adds multiple elements 
  2. Remove Element from Set in Python:

    • Use the remove() method to remove a specific element. If the element is not present, it raises a KeyError. Use discard() to avoid the error.
    # Example my_set = {1, 2, 3, 4} my_set.remove(2) # Removes element 2 my_set.discard(5) # Removes element 5 if present 
  3. Union of Sets in Python:

    • Use the union() method or the | operator for the union of two sets.
    # Example set1 = {1, 2, 3} set2 = {3, 4, 5} union_set = set1.union(set2) # or use set1 | set2 
  4. Set Difference Operation in Python:

    • Use the difference() method or the - operator for the set difference.
    # Example set1 = {1, 2, 3} set2 = {3, 4, 5} difference_set = set1.difference(set2) # or use set1 - set2 
  5. Deleting Elements from a Set in Python:

    • Use the remove() or discard() method to delete specific elements.
    # Example my_set = {1, 2, 3, 4} my_set.remove(2) # Removes element 2 my_set.discard(5) # Removes element 5 if present 
  6. Adding Multiple Elements to a Set in Python:

    • Use the update() method or the | operator to add multiple elements.
    # Example my_set = {1, 2, 3} my_set.update({4, 5, 6}) # or use my_set |= {4, 5, 6} 
  7. Set Intersection Using '&' Operator in Python:

    • Use the intersection() method or the & operator for the set intersection.
    # Example set1 = {1, 2, 3} set2 = {3, 4, 5} intersection_set = set1.intersection(set2) # or use set1 & set2 
  8. Set Union Using '|' Operator in Python:

    • Use the union() method or the | operator for the set union.
    # Example set1 = {1, 2, 3} set2 = {3, 4, 5} union_set = set1.union(set2) # or use set1 | set2 
  9. Symmetric Difference of Sets in Python:

    • Use the symmetric_difference() method or the ^ operator for the symmetric difference.
    # Example set1 = {1, 2, 3} set2 = {3, 4, 5} symmetric_difference_set = set1.symmetric_difference(set2) # or use set1 ^ set2 

More Tags

saga tabnavigator regexp-substr vue.js regex-greedy wildfly-10 baasbox letter tfs sharing

More Programming Guides

Other Guides

More Programming Examples