Python List clear()

Previous
Next

In this tutorial, we will see about Python List‘s clear method.Python List clear method is used to remove all the items from the list.

Python List clear example

You can simply use clear method to delete all the items from the list.Python extend does not return anything but it actually updates the original list.
Let’s understand this with the help of simple example.

l=[1,2,3,4] print("List before clear:",l) l.clear() print("List after clear:",l)

Output:

List before clear: [1, 2, 3, 4]
List after clear: []

If you are using python 3.2 or below, you won’t find clear method in the list.You need to use del operator in that case.

l=[1,2,3,4,5] print("List before clear:",l) del l[:] print("List after clear:",l)

Output:

List before clear: [1, 2, 3, 4, 5]
List after clear: []

That’s all about Python List clear method.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog