Write a Python program to remove item(s) from a given set


The program demonstrates how to remove elements from a set in Python using the pop method.

  • A set n is created using a set literal containing six elements, including duplicates. The print() function is used to display the contents of the set before any elements are removed, which are displayed in curly braces {} and separated by commas.
  • The pop() method is then used to remove an arbitrary element from the set. Since sets are unordered, the specific element that is removed is not predictable. The print() function is used to display the contents of the set after the removal, which now contains five elements.

In this program, the pop method is used to remove an arbitrary element from the set. It is important to note that the pop method removes an arbitrary element, not a specific element, and that sets are unordered, so the specific element that is removed cannot be predicted.

Source Code

n = {30,50,70,20,30,60}
print("Before removing From Set :",n)
n.pop()
print("After removing From Set :",n)

Output

Before removing From Set : {50, 20, 70, 60, 30}
After removing From Set : {20, 70, 60, 30}