How to remove negative numbers from a list in Python
Lists are a vastly used data type in Python to store a collection of values of strings, integers, and even other lists. When using lists in Python, we take into consideration the following key features:
It is mutable and can be modified by adding, removing, or updating its elements.
It is ordered, and we can access the elements through index values.
It can have duplicates of an element.
It is iterable, and we can apply loops to iterate through its elements.
Removing negative numbers
Negative numbers are real numbers that are less than zero and can be stored in a list like positive numbers. There are various ways to identify and remove them from the list to get a list with all positive numbers. Here is the expected output for any list containing negative numbers.
Let's discuss three simple methods to achieve the expected output.
Using a simple loop
We use a for loop to iterate over the list and execute the code in it that is to be applied to every element of the list. In this case, we check if the element is greater than 0 and add it to the new list using append().
#Create a listmyList = [1, -6, 3, -8, -4, 6, 2, 8]print("Input list: " + str(myList))#Implement a looppositiveList = []for element in myList:if element > 0:positiveList.append(element)#Print the new listprint("Output List: " + str(positiveList))
Code explanation
Lines 2–4: Create a list that contains negative and positive values and print it.
Line 7: Create an empty list
positiveListto store the results.Lines 8–10: Iterate over each element in
myListand check if it is greater than 0, then add it to thepositiveList.Line 13: Print the obtained
positiveListon the console.
Using filter()
We use a for loop to iterate over the list and execute the code in it that is to be applied to every element of the list. In this case, we check if the element is greater than 0 and add it to the new list using append().
#Create a listmyList = [1, -6, 3, -8, -4, 6, 2, 8]print("Input list: " + str(myList))#Implement filterpositiveList = list(filter(lambda x : x > 0, myList))#Print the new listprint("Output List: " + str(positiveList))
Code explanation
Lines 2–4: Create a list that contains negative and positive values and print it.
Line 7: Create an empty list
positiveListto store the results.Lines 8–10: Iterate over each element in
myListand check if it is greater than 0, then add it to thepositiveList.Line 13: Print the obtained
positiveListon the console.
Using map()
#Create a listmyList = [1, -6, 3, -8, -4, 6, 2, 8]print("Input list: " + str(myList))#Implement list and maptempList = list(map(str, myList))positiveList = []for i in range(0,len(tempList)):if(not tempList[i].startswith("-")):positiveList.append(myList[i])#Print the new listprint("Output List: " + str(positiveList))
Code explanation
Lines 2–4: Create a list that contains negative and positive values and print it.
Line 7: Use
map()to convert myList to a string and store it intempListinstance.Line 9: Create an empty list
positiveListto store the results.Lines 11–13: Iterate over the
tempListin the givenrange()and if the indexivalue does not start with-then add it to thepositiveList.Line 16: Print the obtained
positiveListon the console.
Comparing methods
Method | Time Complexity | Space Complexity |
Using simple for loop | O(n) | O(n) |
Using filter() | O(n) | O(n) |
Using map() | O(n) | O(n) |
Common query
Can we use list comprehension to remove the negative numbers?
Free Resources