set clear() in python

PythonServer Side ProgrammingProgramming

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.

raja
Published on 11-Jul-2020 06:52:43
Advertisements