w3resource

Python: Remove all elements from a given set


Write a Python program to remove all elements from a given set.

Visual Presentation:

NumPy Sets: Remove all elements from a given set.

Sample Solution:

Python Code:

# Create a set 'setc' with elements "Red", "Green", "Black", and "White".
setc = {"Red", "Green", "Black", "White"}

# Print a message to indicate the original set elements.
print("Original set elements:")

# Print the contents of 'setc'.
print(setc)

# Print a message to indicate the removal of all elements from the set.
print("\nAfter removing all elements of the said set.")

# Use the 'clear()' method to remove all elements from 'setc'.
setc.clear()

# Print 'setc' after clearing, which will be an empty set.
print(setc) 

Sample Output:

Original set elements:
{'Green', 'Black', 'Red', 'White'}

After removing all elements of the said set.
set()

Python Code Editor:

Previous: Write a Python program to create a shallow copy of sets.
Next: Write a Python program to use of frozensets.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.