| by Arround The Web | No comments

Python Clear List

The “Lists” are ordered collections of items that can be modified and accessed by index. These are useful for storing and manipulating data, such as numbers, strings, objects, or other lists. However, sometimes we may need to clear a list, which means removing all the items from it, thereby making it empty. This can be done for various reasons, such as freeing up memory, resetting the list for a new use case, or avoiding unwanted side effects.

This blog will explore different ways to clear a list in Python utilizing relevant examples.

How to Clear List in Python?

Python provides several methods for clearing a list, including the following:

Method 1: Clear List in Python Using “clear()” Method

In Python, the “clear()” method is utilized to remove/delete all the elements/items from the particular list.

Syntax

list.clear()

There is no return value from this method, nor does it take any parameter value.

Example

The following code is utilized to clear all the elements of the list:

list1 = [18, 15, 10, 22, 32]

print('Given List: ', list1)

list1.clear()

print('After Clearing: ', list1)

In the above code, the “list.clear()” method is utilized to clear all the items of the initialized list.

Output

All the list elements have been cleared successfully.

Method 2: Clear List in Python Using the “del” Keyword

In Python, the “del” keyword is utilized to delete/remove objects such as lists, strings, dictionaries, etc.

Example

Here is an example code that is utilized to clear/remove the list:

list1 = [18, 15, 10, 22, 32]

print('Given List: ', list1)

del list1[:]

print('After Clearing: ', list1)

In the above code lines, the “del” keyword is utilized along with the slice notation “ list1[:]” to delete all the elements of the given list. Here the “Slice Notation” means to select all the items from the start till the end of the list.

Output

As seen, all the list items have been cleared successfully.

Method 3: Clear List in Python Using “pop()” Method With “while” Loop

In Python, the “pop()” method is used to remove/delete a particular value from the list. Here, this method is used combined with the “while” loop to clear the list in Python.

Syntax

list.pop(index)

Here the “index” parameter indicates the position of element/items.

Example

The below code is used to clear all the list elements:

list1 = [18, 15, 10, 22, 32]

print('Given List: ', list1)

while(len(list1) != 0):

list1.pop()

print('After Clearing: ', list1)

According to the above code, the “while” loop is used with the “len()” function to check whether the defined list length is zero or not. If the list length is non-zero, the associated “pop()” method removes the elements from the list till it, i.e., list length equals zero.

Output

Method 4: Clear List in Python Using “remove()” Method

The “remove()” method can also be used to clear the list by taking the specified element as an argument.

Syntax

list.remove(element)

Example

In the below code, the “list.remove()” method is used to clear the specified element from the list:

list1 = [18, 15, 10, 22, 32]

print('Given List: ', list1)

list1.remove(10)

print('After Clearing: ', list1)

In this code block, the “remove()” method takes/accepts the specified list item as an argument and eliminates it from the given list.

Output

Here, it can be implied that the specified element from the list is removed instead.

Note: This method cannot remove all the elements from the list at once.

Conclusion

The “clear()” method, “del” keyword, “pop()” method with “while” loop, or the “remove()” method are used to clear the list in Python. The “clear()” method empties all the list elements and returns an empty list, similarly the “del” keyword is used with slicing notation to clear all the list items in Python. The other methods can also efficiently clear all the list items. These methods are explored in this Python blog to effectively clear the list using several examples.

Share Button

Source: linuxhint.com

Leave a Reply