Home »
Python
Python Set clear() Method (with Examples)
Python Set clear() Method: In this tutorial, we will learn about the clear() method of the set class with its usage, syntax, parameters, return type, and examples.
By IncludeHelp Last updated : June 14, 2023
Python Set clear() Method
The clear() is an inbuilt method of the set class that is used to clear/remove all elements of the set. The method is called with this set and returns nothing.
Syntax
The following is the syntax of clear() method:
set_name.clear()
Parameter(s):
The following are the parameter(s):
- None - It does not accept any parameter.
Return Value
The return type of this method is None.
Example 1: Use of Set clear() Method
# declaring a set
cities = {"New Delhi", "Mumbai", "Indore", "Gwalior"}
# printing set before clearing
# the elements
print("cities = ", cities)
# clearing all elements
cities.clear()
# printing set after clearing
# the elements
print("cities = ", cities)
Output
cities = {'New Delhi', 'Mumbai', 'Gwalior', 'Indore'}
cities = set()
Example 2: Use of Set clear() Method
# declaring a set
cities = {'a'}
# printing set before clearing
# the elements
print("cities = ", cities)
# clearing all elements
cities.clear()
# printing set after clearing
# the elements
print("cities = ", cities)
Output
cities = {'a'}
cities = set()