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.

Removing negative values.
Removing negative values.

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 list
myList = [1, -6, 3, -8, -4, 6, 2, 8]
print("Input list: " + str(myList))
#Implement a loop
positiveList = []
for element in myList:
if element > 0:
positiveList.append(element)
#Print the new list
print("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 positiveList to store the results.

  • Lines 8–10: Iterate over each element in myList and check if it is greater than 0, then add it to the positiveList.

  • Line 13: Print the obtained positiveList on 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 list
myList = [1, -6, 3, -8, -4, 6, 2, 8]
print("Input list: " + str(myList))
#Implement filter
positiveList = list(filter(lambda x : x > 0, myList))
#Print the new list
print("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 positiveList to store the results.

  • Lines 8–10: Iterate over each element in myList and check if it is greater than 0, then add it to the positiveList.

  • Line 13: Print the obtained positiveList on the console.

Using map()

#Create a list
myList = [1, -6, 3, -8, -4, 6, 2, 8]
print("Input list: " + str(myList))
#Implement list and map
tempList = list(map(str, myList))
positiveList = []
for i in range(0,len(tempList)):
if(not tempList[i].startswith("-")):
positiveList.append(myList[i])
#Print the new list
print("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 in tempList instance.

  • Line 9: Create an empty list positiveList to store the results.

  • Lines 11–13: Iterate over the tempList in the given range() and if the index i value does not start with - then add it to the positiveList.

  • Line 16: Print the obtained positiveList on 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

Question

Can we use list comprehension to remove the negative numbers?

Show Answer

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved