# Python Set Modification

## Adding Set Items

In Python, you can add items to a set using the add() method. This method takes a single argument, which is the item to be added to the set. Here is an example:

# Initialize a set my_set = {1, 2, 3} # Add an item to the set my_set.add(4) # The set now contains the items 1, 2, 3, and 4 print(my_set) # Output: {1, 2, 3, 4}

You can also add multiple items to a set at once using the update() method. This method takes an iterable (such as a list or another set) as an argument and adds all the items from that iterable to the set. Here is an example:

# Initialize a set my_set = {1, 2, 3} # Add multiple items to the set my_set.update([4, 5, 6]) # The set now contains the items 1, 2, 3, 4, 5, and 6 print(my_set) # Output: {1, 2, 3, 4, 5, 6}

## Removing Set Items

To remove items from a set, you can use the remove() method. This method takes a single argument, which is the item to be removed from the set. If the item is not present in the set, the remove() method will raise a KeyError exception. To avoid this, you can use the discard() method, which does the same thing as remove() but does not raise an exception if the item is not present in the set.

Here is an example of using remove() and discard():

# Initialize a set my_set = {1, 2, 3, 4, 5} # Remove an item from the set using remove() my_set.remove(4) # The set now contains the items 1, 2, 3, and 5 print(my_set) # Output: {1, 2, 3, 5} # Remove an item from the set using discard() my_set.discard(6) # The set remains unchanged print(my_set) # Output: {1, 2, 3, 5}

You can also remove all items from a set using the clear() method. This method does not take any arguments and removes all items from the set, leaving it empty. Here is an example:

# Initialize a set my_set = {1, 2, 3, 4, 5} # Remove all items from the set my_set.clear() # The set is now empty print(my_set) # Output: set()

## Combining Sets

There are several ways to combine two or more sets in Python. One way is to use the union() method, which returns a new set that contains all the items from both sets. Here is an example:

# Initialize two sets set_1 = {1, 2, 3} set_2 = {3, 4, 5} # Combine the sets using union() combined_set = set_1.union(set_2) # The combined set contains all the items from both sets print(combined_set) # Output: {1, 2, 3, 4, 5}

Another way to combine sets is to use the update() method, which adds all the items from one set to another. This method modifies the set in place, so it does not return a new set. Here is an example:

# Initialize two sets set_1 = {1, 2, 3} set_2 = {3, 4, 5} # Combine the sets using update() set_1.update(set_2) # The set_1 now contains all the items from both sets print(set_1) # Output: {1, 2, 3, 4, 5}

You can also use the | operator to combine sets. This operator returns a new set that contains all the items from both sets. Here is an example:

# Initialize two sets[python-numeric-types.md](python-numeric-types.md) set_1 = {1, 2, 3} set_2 = {3, 4, 5} # Combine the sets using the | operator combined_set = set_1 | set_2 # The combined set contains all the items from both sets print(combined_set) # Output: {1, 2, 3, 4, 5}