Methods and Functions

Python Set clear()

To clear all of the data from the set data structure, use the python set clear method. All of the elements from the set will be cleared. We’ll be leaving with an empty set.

To see a clear technique in action, follow the steps below.

  • Create a new set.
  • Clear the set with the clear method.
  • When you print the set, you’ll notice that one of the boxes is blank.

Source

Syntax:

set.clear()

Set clear() Parameters

The clear() method has no parameters.

Return value from Set clear()

This method has no return value; it simply removes all of the elements from the set.

Example 1: Remove all elements from a Python set using clear()

# set of vowels
vowels = {'a', 'e', 'i', 'o', 'u'}
print('Vowels (before clear):', vowels)

# clearing vowels
vowels.clear()
print('Vowels (after clear):', vowels)

Output

Vowels (before clear): {'e', 'a', 'o', 'u', 'i'}
Vowels (after clear): set()

Questions
How do I remove a set in Python?
There are 2 methods to remove a set in Python: discard() method and remove() method.
Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.