Remove Negative Elements in List-Python
Last Updated :
11 Feb, 2025
The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive numbers. For example, if a = [5, 6, -3, -8, 9, 11, -12, 2], the resulting list after removing negative numbers would be [5, 6, 9, 11, 2].
Using list comprehension
List comprehension is the most concise and Pythonic way to filter out negative numbers. It iterates through the list, applying a condition (ele > 0) to include only positive values. This method is widely used due to its readability and efficiency in handling small to moderately large lists.
Python
a = [5, 6, -3, -8, 9, 11, -12, 2]
res = [ele for ele in a if ele > 0]
print(res)
Explanation: list comprehension iterates through each element, keeping only those greater than 0
, and stores them in res
.
Using filter()
filter() applies a boolean condition (lambda x: x > 0) to each element, returning an iterator with only positive numbers. It is a functional programming approach that enhances code clarity and efficiency, especially when working with built-in filtering methods.
Python
a = [5, 6, -3, -8, 9, 11, -12, 2]
res = list(filter(lambda x: x > 0, a))
print(res)
Explanation: filter() with a lambda function to retain only positive numbers from a, converting the result to a list.
compress() filters a list based on a predefined boolean mask ([x > 0 for x in a]). It is useful when working with complex filtering conditions or when a separate boolean mask needs to be used for multiple operations. While not as common as list comprehension, it is an optimized method for selective filtering.
Python
from itertools import compress
a = [5, 6, -3, -8, 9, 11, -12, 2]
mask = [x > 0 for x in a]
res = list(compress(a, mask))
print(res)
Explanation: itertools.compress() with a boolean mask to filter out negative numbers. The mask [x > 0 for x in a] marks positives as True and compress(a, mask) retains only those values.
Using numpy
NumPy provides an optimized, vectorized approach for filtering numerical data. Using a[a > 0] efficiently extracts positive elements without explicit loops. This method is ideal for large datasets, offering high-speed performance compared to traditional Python lists.
Python
import numpy as np
# Creating a NumPy array
a = np.array([5, 6, -3, -8, 9, 11, -12, 2])
# Creating a NumPy array
res = a[a > 0]
print(res.tolist()) # Convert back to list if needed
Explanation: This code selects only positive numbers from a using NumPy's indexing (a[a > 0]), which is faster than loops. The result is then converted into a list using .tolist().
Similar Reads
Remove Multiple Elements from List in Python In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 min read
Python - Odd elements removal in List Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
5 min read
Remove last K elements of list - Python Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python.Using list slicingList slicing is one of
3 min read
Python - Remove elements at Indices in List In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index. For example:Input : li= [5, 6, 3, 7
2 min read
Python - Remove non-increasing elements Given a list, our task is to write a Python program to remove all the non-increasing elements from the list. Input : test_list = [5, 3, 4, 5, 7, 3, 9, 10, 3, 10, 12, 13, 3, 16, 1] Output : [5, 5, 5, 7, 9, 10, 10, 12, 13, 16] Explanation : 3, 4 are omitted as 5, (greater element) had occurred before
3 min read