set clear() in python


In this tutorial, we are going to learn about the clear method of set data structure. Let's see details about the clear method.

The method clear is used to clear all the data from the set data structure. All elements from set will clear. And we will leave with an empty set.

Follow the below steps to see clear method in action.

  • Initialize a set.
  • User the clear method and clear the set.
  • Print the set and you will see an empty one.

Example

 Live Demo

# initialzing a set
number_set = {1, 2, 3, 4, 5}
# clearing the set
number_set.clear()
# printing the set
print(number_set)

Output

If you run the above code, then you will get the following result.

set()

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Updated on: 11-Jul-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements