Methods and Functions

Python List clear()

Python offers you a range of built-in functions to remove elements from a list, such as pop(), remove(), del, etc. However, if you want to remove all the items in a list at once, you can use the Python clear list method.

python clear list

clear() Parameters

The clear() method has the simple syntax of:

Unlike other deleting functions, clear() does not accept any parameters.

Return Value from clear()

The method also does not return any value and only removes all the elements in a list.

Example of how clear() method works

Let us look at an example where we will clear all the items in the list: hobbies.

                    

# create list
hobbies = ['swimming', 'singing', 'dancing', 'running', 'painting']
hobbies.clear()

print ('List:', hobbies)

Output:

Example of emptying the List Using del

Although the Python clear list function is convenient, it is not available in Python 2 or Python 3.2 and below. In these versions, we use the del keyword.

                    

# hobbies list
hobbies = ['swimming', 'singing', 'dancing', 'running', 'painting']
del hobbies[:]

print ('List:', hobbies)

Output:

Questions and Answers

Q1. How do you clear a list in Python?

You can clear a list in Python by using the Python clear() list method.

Let us see how:

                    

# creating a fruits list
fruits = ['apple', 'grapes', 'grapes', 'banana', 'guava', 'grapes', 'banana']

# clearing all the elements using the clear() method
fruits. clear()

print ('Fruits:', fruits)

Output:

Q2. What does clear() do in Python?

Python clear for lists is a built-in function that allows you to remove all the items in a list at once. It neither accepts any parameters nor returns a value. It follows the syntax:

Q3. How do you empty a list?

Apart from using the Python clear list function, you can also empty a list using the del keyword. Usually, we pass the index values of the list element to delete them through del or set a range of indexes using a colon to delete multiple items.

However, to empty the complete list, we enclose the colon in [] without index values. Let us see how this through an example:

                    

# creating a flowers list 
flowers = ['rose', 'lily', 'tulip', 'lotus',  'marigold']

# clearing all the elements using the del keyword
del fruits [ : ]

print('Flowers:', flowers)

Output:

Q4. How do I remove all elements from a list?

You can remove all the elements from a list through three methods:

1. By using the clear() function.

2. By using the del keyword.

3. By using the slice assignment.

Let us see how:

                    

# creating name list
name = ['Bob', 'Phil', 'Alice', 'Noah', 'Jill', 'Jack', 'David']

# removing all elements using clear()
name.clear()
print ('Names list:',name)

# creating subjects list
subject = ['Science', 'Maths', 'English', ' Social']

# removing all elements using del
del subject [ : ]
print ('Subjects list:', subject)

# creating number list
num = [43, 54, 8, 3, 345, 34,]
x = num
print (num)	# prints [43, 54, 8, 3, 345, 34,]

num[ : ] = []	# assigning an empty list to num
print ('Numbers list:', num)	# prints []
print (x)		# prints []

Output:

                    

Names list: []
Subjects list: []
[43, 54, 8, 3, 345, 34]
Numbers list: []
[]

In the final example, we cleared the entire num list by assigning an empty list to the slice, i.e, num[ : ] = [].

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.