Python Sets

Built-in Set methods

Sets in Python come with various methods that allow you to modify and interact with their elements. Here's a breakdown of some commonly used methods:


1. Adding elements

  • add(element)
This method adds a new element to the set. If the element already exists, it's ignored (no duplicates allowed).

fruits = {"apple", "banana"}
fruits.add("orange")  # Adds "orange" to the set

print(fruits)  # Output: {'apple', 'orange', 'banana'}


2. Removing elements

  • remove(element)
This method removes the specified element from the set. If the element is not found, it raises a KeyError.

fruits = {"apple", "banana", "orange"}
fruits.remove("banana")  # Removes "banana"

print(fruits)  # Output: {'apple', 'orange'}


  • discard(element)
This method is similar to remove but doesn't raise an error if the element is not found. It silently ignores the removal request.

fruits = {"apple", "orange"}
fruits.discard("mango")  # No error, "mango" not present

print(fruits)  # Output: {'apple', 'orange'} (Unchanged)


  • pop()
This method removes and returns an arbitrary element from the set. However, it raises a KeyError if the set is empty.

fruits = {"apple", "orange"}
removed_fruit = fruits.pop()  # Removes and returns an element (e.g., "apple")

print(fruits)  # Output: {'orange'}
print(removed_fruit)  # Output: "apple" (Example)


3. Modifying the Set

  • clear()
This method removes all elements from the set, essentially making it empty.

fruits = {"apple", "orange"}
fruits.clear()

print(fruits)  # Output: set() (Empty set)


  • update(iterable)
This method updates the set by adding elements from an iterable (like a list, tuple, or another set). Duplicates are removed during the update.

fruits = {"apple", "orange"}
new_fruits = ["mango", "banana", "apple"]
fruits.update(new_fruits)  # Adds elements from the list

print(fruits)  # Output: {'apple', 'orange', 'mango', 'banana'} (Duplicates removed)


4. Set update methods (update the set based on operations)

  • intersection_update(iterable)
This method updates the set with the intersection of itself and another iterable. It keeps only elements common to both sets.

fruits = {"apple", "orange", "mango"}
available_fruits = {"apple", "banana", "cherry"}
fruits.intersection_update(available_fruits)  # Keeps only common fruits

print(fruits)  # Output: {'apple'}


  • difference_update(iterable)
This method updates the set with the difference of itself and another iterable. It keeps elements present in the first set but not in the second.

fruits = {"apple", "orange", "mango"}
unavailable_fruits = {"orange", "grape"}
fruits.difference_update(unavailable_fruits)  # Removes unavailable fruits

print(fruits)  # Output: {'apple', 'mango'}


  • symmetric_difference_update(iterable)
This method updates the set with the symmetric difference of itself and another iterable. It keeps elements that are unique to either set.

fruits = {"apple", "orange", "mango"}
available_fruits = {"apple", "banana", "cherry"}
fruits.symmetric_difference_update(available_fruits)  # Keeps fruits unique to each set

print(fruits)  # Output: {'orange', 'mango', 'cherry'}


Practice scenarios:

1. Removing duplicates from a list:

numbers = [1, 2, 2, 3, 4, 1]
unique_numbers = set(numbers)
print(unique_numbers)  # Output: {1, 2, 3, 4} (Duplicates removed)

2.Finding common elements between sets:

set1 = {"apple", "banana", "cherry"}
set2 = {"mango", "banana", "grape"}
common_fruits = set1.intersection(set2)
print(common_fruits) # Output {'banana'}

It's time to take a quiz!

Test your knowledge and see what you've just learned.

What will be the output of the following code: fruits = {"apple", "banana"}; fruits.add("orange"); print(fruits)?

{"apple", "banana"}
{"apple", "orange", "banana"}
{"apple", "banana", "orange"}
{"banana", "orange"}
Check Answer

What will be the output after executing: fruits = {"apple", "banana", "orange"}; fruits.remove("banana"); print(fruits)?

{"banana", "orange"}
{"apple", "orange"}
{"apple", "banana"}
{"apple", "banana", "orange"}
Check Answer

What happens if you execute: fruits = {"apple", "orange"}; fruits.discard("mango"); print(fruits)?

{"apple", "orange"}
{"apple"}
{"orange"}
{"mango", "apple", "orange"}
Check Answer

What will the output be after executing: fruits = {"apple", "orange"}; fruits.clear(); print(fruits)?

{"apple", "orange"}
{}
set()
None
Check Answer

What will be the output after executing: fruits = {"apple", "orange"}; fruits.update(["banana", "apple"]); print(fruits)?

{"orange", "banana"}
{"apple", "orange", "banana", "apple"}
{"apple", "orange", "banana"}
{"banana", "apple"}
Check Answer