×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Set clear() Method (with Examples)

Last Updated : December 11, 2025

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()
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.