Python set clear method

Python set clear function is one of the set methods used to clear or remove total set items. In this section, we discuss how to use this Python clear set function with practical examples.

The basic syntax behind this set clear function is as:

set_Name.clear()

Python set clear Example 1

The set clear function helps you to clear or remove the total number of items in a given set. The below code uses a set clear method to remove items from a clearSet (string set).

# Python Set clear method

clearSet = {'apple', 'Mango', 'kiwi', 'orange', 'banana'}
print("\nOld Set Items = ", clearSet)

# Python Clear Set Items
clearSet.clear()
print("After Clear Method = ", clearSet)
Python set clear method 1

Python set cleat example 2

# Python Set clear method

clearIntSet = {15, 25, 35, 45, 55}
print("\nOld Set Items = ", clearIntSet)

# Python Clear Set Items
clearIntSet.clear()
print("After Clear Method = ", clearIntSet)
Old Set Items = {35, 45, 15, 55, 25}
After Clear Method = set()

TIP: Please refer to the Python set article to understand everything about Sets.