JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Deleting multiple elements from a list in Python

Author:JIYIK Last Updated:2025/05/09 Views:

To remove multiple values ​​from a Python list, we can either remove the actual values ​​of the list or the index of the values ​​to be removed from the list. We can use if...elsecontrol statements, list comprehensions, list slicing, and forloops to remove multiple elements from a list in Python.


Use if...elsethe control statement to remove multiple elements from a list

We can use if...elsecontrol statements to remove multiple values ​​from a list in Python if the values ​​meet a certain condition. We can also remove an element if its index meets a certain condition.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20]
print("Original list : ", list1)


for ele in list1:
    if (ele % 2) != 0:
        list1.remove(ele)

print("List after deleting all values which are odd : ", list1)

Output:

Original list :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20]
List after removing all the odd values :  [2, 4, 6, 8, 10, 20]

Here, we have a list of integers and we need to remove the elements having odd values ​​from the list. In this case, we loop through all the list l1elements and if the element has an odd value, then we remove()remove that element from the list using the remove method.


Remove multiple elements from a list using list comprehension in Python

In Python, list comprehension refers to the process of generating a new list from an existing list. List comprehensions can also be used to remove multiple elements from a list. We can create a new list by removing the values ​​to be removed from the original list.

l1 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 80, 99]
l2 = [x for x in l1 if x % 2 == 0]

print("Original list : ", l1)
print("List after deleting all the odd values present in List : ", l2)

Output:

Original List :  [2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 80, 99]
List after deleting all the odd values present in List :  [2, 4, 6, 8, 10, 20, 80]

Here, we l1create a list from the list l2that contains only l1all the even values ​​in . In other words, we can say that we created a new list l1from the existing list by removing all the odd values ​​in the list .l1l2


Remove multiple elements from a list using list slicing

We can also remove multiple elements from a list using List slice method. Here, we can delput the range of elements from the start index to the last index of the elements that you want to remove from the list in the method.

Instead of using a single index in the remove() method, we deluse a range of values ​​from the start to the last index of the element to be removed from the list. It will remove consecutive elements from the list. We have to note that for lists, the indexing of values ​​in Python starts from 0.

l1 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 80, 99]
print("Original list : ", l1)

del l1[2:5]

print("List after removing values at index 2, 3 and 4 : ", l1)

Output:

Original List :  [2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 80, 99]
List after removing values at index 2, 3 and 4 :  [1, 2, 6, 7, 8, 9, 10, 20, 80, 99]

It uses delthe method l1to remove the values ​​at index 2, 3and from the list 4.
An important thing to note here is that the first index 2is inclusive, i.e. the element at 3index in the list is removed, whereas the last index is exclusive, which means the element at 2index in the list is not removed.56


Use forloop to remove multiple elements from a list

We can also use fora loop to remove multiple elements from a list.
To apply this method, we have to store the index of the elements to be removed from the list. But random removal of elements will result in changing the value of the index. Our smart strategy is to remove the elements which have a greater position in the list. So the values ​​of elements at other indexes will not change. Now, we will sort the list in descending order to remove the elements with higher index first. The code for this approach is shown below.

l1 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 80, 99]
print("Original list : ", l1)

indexes_to_be_removed = [0, 2, 5]

for idx in sorted(indexes_to_be_removed, reverse=True):
    del l1[idx]

print("List after removing values at index 0, 2 and 5: ", l1)

Output:

Original List :  [2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 80, 99]
List after removing values at index 0, 2 and 5:  [3, 5, 6, 8, 9, 10, 20, 80, 99]

It uses delthe method l1to remove the values ​​at indexes 0, 2, and from the list 5.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding a string in a list in Python

Publish Date:2025/05/09 Views:75 Category:Python

This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem

Getting list shape in Python

Publish Date:2025/05/09 Views:139 Category:Python

In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim

Adding multiple elements to a list in Python

Publish Date:2025/05/09 Views:180 Category:Python

List is a mutable data structure in Python. It can contain values ​​of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi

Get the index of the maximum and minimum values in a list in Python

Publish Date:2025/05/09 Views:117 Category:Python

In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi

List of numbers from 1 to N in Python

Publish Date:2025/05/09 Views:116 Category:Python

This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until

Convert List to Pandas DataFrame in Python

Publish Date:2025/05/09 Views:133 Category:Python

This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i

Sorting a list by another list in Python

Publish Date:2025/05/09 Views:148 Category:Python

Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python. We will learn how to sort a given list based on the values ​​in another list in this art

Normalizing a list of numbers in Python

Publish Date:2025/05/09 Views:134 Category:Python

Normalization means converting the given data to another scale. We rescale the data so that it is between two values. Most of the time, the data is rescaled between 0 and 1. We rescale data for different purposes. For example, machine learn

How to create a list of a specific size in Python

Publish Date:2025/05/09 Views:195 Category:Python

Preallocating storage for a list or array is a common practice among programmers when they know the number of elements in advance. Unlike C++ and Java, in Python you must initialize all preallocated storage with some value. Typically, devel

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial