Table of Contents
The clear() method removes all elements from the set.
The syntax of clear() method is:
set.clear()
1. Set clear() Parameters
clear() method doesn’t take any parameters.
2. Return value from Set clear()
clear() method doesn’t return any value and returns a None.
3. 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()
Related posts:
Python Program to Print the Fibonacci sequence
Python Errors and Built-in Exceptions
Python String capitalize()
Python str()
Python Program to Check Leap Year
Python Program to Create a Long Multiline String
Converting Between a List and a Set in Java
Python enumerate()
Python String find()
Python if...else Statement
Python String expandtabs()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python sum()
Python while Loop
Python Program to Merge Two Dictionaries
Python Program to Slice Lists
Python pow()
Python Set update()
Python type()
Python Set difference()
Python staticmethod()
Python String ljust()
Python String encode()
Python int()
Python String count()
Python Inheritance
Python round()
Python Input, Output and Import
Python @property decorator
Python Set add()
Python Program to Solve Quadratic Equation
Python List extend()