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.
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.
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))
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.
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))
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.
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))
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.
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) |
Can we use list comprehension to remove the negative numbers?
Free Resources