Sometimes, while working with Python lists, we can have a problem in which we need to remove all the negative elements from list. This kind of problem can have application in many domains such as school programming and web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [6, 4, 3]
Output : [6, 4, 3]
Input : test_list = [-6, -4]
Output : []
Method #1: Using list comprehension The combination of above functions can be used to solve this problem. In this, we perform the task of removing negative elements by iteration in one liner using list comprehension
Python3
test_list = [ 5 , 6 , - 3 , - 8 , 9 , 11 , - 12 , 2 ]
print ("The original list is : " + str (test_list))
res = [ele for ele in test_list if ele > 0 ]
print (" List after filtering : " + str (res))
|
Output :
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
Time complexity: O(n) where n is the size of the input list.
Auxiliary space: O(n), because it creates a new list to store the filtered result, which can be up to the size of the input list.
Method #2 : Using filter() + lambda The combination of above functions can also offer an alternative to this problem. In this, we extend logic of retaining positive formed using lambda function and extended using filter().
Python3
test_list = [ 5 , 6 , - 3 , - 8 , 9 , 11 , - 12 , 2 ]
print ("The original list is : " + str (test_list))
res = list ( filter ( lambda x : x > 0 , test_list))
print (" List after filtering : " + str (res))
|
Output :
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. filter() + lambda performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #3: Using list(),map(),startswith() methods
Python3
test_list = [ 5 , 6 , - 3 , - 8 , 9 , 11 , - 12 , 2 , 0 ]
print ( "The original list is : " + str (test_list))
x = list ( map ( str ,test_list))
res = []
for i in range ( 0 , len (x)):
if ( not x[i].startswith( "-" ) and x[i]! = "0" ):
res.append(test_list[i])
print ( "List after filtering : " + str (res))
|
Output
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), where n is the number of elements in the input list.
Method #4:Using a for loop
- Create an empty list res to store the filtered elements.
- Iterate over each element ele in the input list test_list.
- For each element, check if it is greater than 0.
- If the element is greater than 0, add it to the res list.
- Return the res list containing only the positive integers from the input list.
Python3
test_list = [ 5 , 6 , - 3 , - 8 , 9 , 11 , - 12 , 2 ]
print ( "The original list is : " + str (test_list))
res = []
for ele in test_list:
if ele > 0 :
res.append(ele)
print ( "List after filtering : " + str (res))
|
Output
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
Time Complexity: O(n)
We iterate over each element in the input list once, which takes O(n) time where n is the length of the list.
Auxiliary Space: O(n)
We create an additional list res to store the filtered elements, which takes up to O(n) space where n is the length of the list.
Method #6: Using the itertools module and the filter() function
- Import the itertools module.
- Initialize the list test_list.
- Use the filter() function with a lambda function that checks if an element is positive to create a new list res.
- Print the resulting list res.
Python3
import itertools
test_list = [ 5 , 6 , - 3 , - 8 , 9 , 11 , - 12 , 2 ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: x > 0 , test_list))
print ( "List after filtering : " + str (res))
|
Output
The original list is : [5, 6, -3, -8, 9, 11, -12, 2]
List after filtering : [5, 6, 9, 11, 2]
Time complexity: O(n)
Auxiliary space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Apr, 2023
Like Article
Save Article