| by Arround The Web | No comments

Remove Item From List Python

Python works with different types of data structures, such as “lists”. A “list” is an ordered collection of elements/items that can be modified, accessed, and iterated over. Sometimes, we want to remove items/elements from the Python list for various reasons, such as cleaning up specified data, filtering out unwanted values, or optimizing the code.

This Python write-up presents a detailed guide on removing elements/items from a list.

How to Remove/Eliminate Items From Python List?

The following methods are used to eliminate an item from a specified list:

Method 1: Remove an Item From a List Using the “list.remove()” Method

The “list.remove()” method is utilized to remove/eliminate the particular list item/element.

Syntax

list.remove(element)

 

Example

The below example code is intended to remove a list item:

list1 = [20, 30, 50, 70, 55]
print('Given List: ', list1)
list1.remove(30)
print('\nAfter Removing List Item: ', list1)

 

In the above code, the “list.remove()” method takes the target element as its argument and removes it from the initialized list.

Output

The item has been removed from the given list.

Method 2: Remove an Item From a List Utilizing the “pop()” Method

The “pop()” method accepts the index of the specified element/item as an argument and deletes it from the given list.

Syntax

list.pop(x)

 

Here, the “x” parameter specifies the element’s index that needs to be deleted/removed.

Example

This example code is utilized to remove/delete a list item:

list1 = [20, 30, 50, 70, 55]
print('Given List: ', list1)
list1.pop(4)
print('\nAfter Removing List Item: ', list1)

 

According to the above code, the “list.pop()” method takes the index “4” as an argument and removes the corresponding element value placed at this index.

Output

As seen, the particular item has been removed from the initialized list.

Method 3: Remove All the List Items Using the “clear()” Method

In Python, the “clear()” method is used to clear all the items/elements from the list.

Syntax

list.clear()

 

Example

The below code block is intended to eliminate all the list items:

list1 = [20, 30, 50, 70, 55]
print('Given List: ', list1)
list1.clear()
print('\nAfter Removing List Item: ', list1)

 

In the above code, the “list.clear()” method removes all the items/elements from the defined list appropriately.

Output

The above output verified that all the list items had been removed.

Method 4: Remove an Item From a List Utilizing the “del” Keyword

The “del” keyword is employed along with the square notation to delete/remove an item/element from a list.

Example

The below-stated example code is used to remove/delete an item from a list:

list1 = [20, 30, 50, 70, 55]
print('Given List: ', list1)
del list1[2]
print('\nAfter Removing List Item: ', list1)

 

In the above code lines, the “del” keyword is used along with the square notation i.e., “[2]” to remove the target item/element from the list based on the specified index.

Output

Method 5: Remove an Item From a List Using “List Comprehension”

The “List Comprehension” approach can also delete/remove an item from a specified list.

Example

Here is an example code:

list1 = [20, 30, 50]
print('Given List: ', list1)
list1 = [i for i in list1 if i != 30]
print('\nAfter Removing List Item: ', list1)

 

According to the above code, the “List Comprehension” approach removes an element/item from the list by using the “for” loop and “if” condition with the “!=” operator.

Output

Here, it can be implied that the target element has been removed from the list.

Conclusion

The “list.remove()” method, “pop()” method, “clear()” method, “del” keyword, or the “List Comprehension” approach are used to remove the item from the list. These approaches either remove the specified or all the items from a list. This write-up provided various ways to remove the item(s) from a list utilizing multiple examples.

Share Button

Source: linuxhint.com

Leave a Reply