Python Set clear method with Example – Python Tutorial

1 year ago Lalit Bhagtani 0

In this tutorial, we will learn about python set clear method.
Python Set Clear

clear method :

This method is used to remove all the elements from the set object.

Syntax : < Set Object >.clear( ) 

Example:

[datacamp_exercise lang=”python” height=”300″ min-height=”200″ max-height=”500″]
[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]
[datacamp_sample_code]
# create set object and assign it to variable s
s = {1,2,3,4,5}

# print all elements of set
print(s)

# call clear() method to remove all the elements from s
s.clear()

# print all elements of set
print(s)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[/datacamp_exercise]

References :-

  1. clear method Docs

That’s all for Python Set clear method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous