Tutorial Study Image

Python clear()

The clear() function in python helps to remove all the elements from the list. After this method calling the list gets initialized to an empty object.


list.clear() #where list in which needs to clear
 

clear() Parameters:

The clear() method doesn't take any parameters. We can say that this method is equivalent to del a[:].

clear() Return Value

This method doesn't return any value. It just makes the list empty.

Examples of clear() method in Python

Example 1:How clear() method Working in Python?


# Defining a list
org_list = [{5, 6}, ('x'), ['5.5', '6.6']]

# clearing the list
org_list.clear()

print('List after clear:', org_list)
 

Output:


List after clear: []

Note: We can use the del operator instead of clear(),in case of using Python 2 or Python 3.2 and below

Example 2:How to emptying the List using del in Python?


# Defining a list
org_list = [{5, 6}, ('x'), ['5.5', '6.6']]

# clearing the list
del org_list[:]

print('List after delete:', org_list)
 

Output:


List after delete: []