Python List clear()

Python list clear() | In this post, we will discuss how to clear all items or elements from the list using the clear() method. Python dictionary method clear() removes all the elements from the list. It clears the list completely and returns nothing. It does not require any parameter and returns no exception if the list is already empty. The clear() method only empties the given list.

The Syntax of the clear() method is:

list_name.clear()

Parameters:

The clear() method does not take any parameters.

Return Value:

The clear() method does not return any value (returns None). This method only empties the given list.

Note: The clear() method only works in Python’s latest version. If you are using Python 2 or Python 3.2 and below, the clear() method does not work.

Python clear Array

We will take the list while declaring the variables then, the Python program removes all elements from the list using my_list.clear(). Finally, the new empty list will be displayed on the screen.

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed all item from the list
my_list.clear()

# print list after all item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [ ]

Also See:- Python List remove()

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *