Account
Categories

Python Set clear() Method


Definition

When you use the clear() method in your program, it removes all elements from the set. After removing all elements, the set becomes empty.

Now you will clearly understand the method through the flowchart.

s = {9, 2, 30}

clear()
   │
   ▼
{9, 2, 30}
   │
   ▼
9   2   30
❌  ❌  ❌
   │
   ▼
s = { }   (empty set)

Syntax:

set_name.clear()

Example:

s = {9, 2, 30}
s.clear()
print(s)

Output:

Note:

In Python, when all elements of a set are deleted, the set becomes empty, represented as set().

Exercise

Create a set s = {1, 2, 3} and clear all elements.