Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python - Remove List Items



Removing List Items

Removing list items in Python implies deleting elements from an existing list. Lists are ordered collections of items, and sometimes you need to remove certain elements from them based on specific criteria or indices. When we remove list items, we are reducing the size of the list or eliminating specific elements.

We can removelist items in Python using various methods such asremove(),pop() and clear(). Additionally, we can use thedel statement to remove items at a specific index. Let us explore through all these methods in this tutorial.

Remove List Item Using remove() Method

The remove() method in Python is used to remove the first occurrence of a specified item from a list.

We can remove list items using the remove() method by specifying the value we want to remove within the parentheses, likemy_list.remove(value), which deletes the first occurrence of value frommy_list.

Example

In the following example, we are deleting the element "Physics" from the list "list1" using the remove() method −

list1 = ["Rohan", "Physics", 21, 69.75]print ("Original list: ", list1)list1.remove("Physics")print ("List after removing: ", list1)

It will produce the following output −

Original list: ['Rohan', 'Physics', 21, 69.75]List after removing: ['Rohan', 21, 69.75]

Remove List Item Using pop() Method

The pop() method in Python is used to removes and returns the last element from a list if no index is specified, or removes and returns the element at a specified index, altering the original list.

We can remove list items using the pop() method by calling it without any argumentsmy_list.pop(), which removes and returns the last item frommy_list, or by providing the index of the item we want to removemy_list.pop(index), which removes and returns the item at that index.

Example

The following example shows how you can use the pop() method to remove list items −

list2 = [25.50, True, -55, 1+2j]print ("Original list: ", list2)list2.pop(2)print ("List after popping: ", list2)

We get the output as shown below −

Original list: [25.5, True, -55, (1+2j)]List after popping: [25.5, True, (1+2j)]

Remove List Item Using clear() Method

The clear() method in Python is used to remove all elements from a list, leaving it empty.

We can remove all list items using the clear() method by calling it on the list object likemy_list.clear(), which emptiesmy_list, leaving it with no elements.

Example

In this example, we are using the clear() method to remove all elements from the list "my_list" −

my_list = [1, 2, 3, 4, 5]# Clearing the listmy_list.clear()print("Cleared list:", my_list)

Output of the above code is as follows −

Cleared list: []

Remove List Item Using del Keyword

The del keyword in Python is used to delete element either at a specific index or a slice of indices from memory.

We can remove list items using the del keyword by specifying the index or slice of the items we want to delete, likedel my_list[index] to delete a single item or delmy_list[start:stop] to delete a range of items.

Example

In the below example, we are using the "del" keyword to delete an element at the index "2" from the list "list1" −

list1 = ["a", "b", "c", "d"]print ("Original list: ", list1)del list1[2]print ("List after deleting: ", list1)

The result produced is as follows −

Original list: ['a', 'b', 'c', 'd']List after deleting: ['a', 'b', 'd']

Example

In here, we are deleting a series of consecutive items from a list with the slicing operator −

list2 = [25.50, True, -55, 1+2j]print ("List before deleting: ", list2)del list2[0:2]print ("List after deleting: ", list2)

It will produce the following output −

List before deleting: [25.5, True, -55, (1+2j)]List after deleting: [-55, (1+2j)]
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp