Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
72 views
in Python by (114k points)
How to clear a set in Python?

Please log in or register to answer this question.

1 Answer

0 votes
by (114k points)

In Python, you can clear a set by calling the clear() method on the set. 

Here is an example:

my_set = {1, 2, 3, 4}
print(my_set)  # Output: {1, 2, 3, 4}

my_set.clear()
print(my_set)  # Output: set()
 

In the above example, my_set is initialized with the values {1, 2, 3, 4}. The clear() method is then called on the set to remove all its elements. Finally, the empty set is printed to confirm that the set has been cleared.

Note that calling the clear() method on a set modifies the original set and does not create a new set.

Related questions

0 votes
1 answer
asked Apr 4, 2023 in Python by kvdevika (114k points)
0 votes
1 answer
0 votes
1 answer
asked Apr 4, 2023 in Python by kvdevika (114k points)
0 votes
1 answer
0 votes
1 answer
asked Mar 22, 2023 in Python by kvdevika (114k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...