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 , 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(). ,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.,Method #3: Using list(),map(),startswith() methods
The original list is: [5, 6, -3, -8, 9, 11, -12, 2] List after filtering: [5, 6, 9, 11, 2]
The original list is: [5, 6, -3, -8, 9, 11, -12, 2] List after filtering: [5, 6, 9, 11, 2]
It's generally a bad idea to remove elements from a list while iterating over it (see the link in my comment for an explanation as to why this is so). A better approach would be to use a list comprehension:
num_list = [item for item in num_list if item >= 0 ]
Notice that the line above creates a new list and assigns num_list
to that. You can also do an "in-place" assignment of the form
num_list[: ] = ...
Much simpler:
>>> a = [1, 2, 3, -3, 6, -1, -3, 1] >>> [x for x in a if x >= 0 ] [1, 2, 3, 6, 1]
If you really do want to loop, try this:
def remove_negs(num_list):
r = num_list[: ]
for item in num_list:
if item < 0:
r.remove(item)
print r
This does what you want:
>>> remove_negs([1, 2, 3, -3, 6, -1, -3, 1])[1, 2, 3, 6, 1]
After this statement is run, r
and num_list
both point to the same data. If you make a change to r
's data, you are also making a change to num_list
's data because they both point to the same data. Now, consider:
r = num_list[: ]
Examples:
>>> a = [1, 2, 3, -3, 6, -1, -3, 1] >>> b = a # a and b now point to the same place >>> b.remove(-1) >>> a[1, 2, 3, -3, 6, -3, 1]
Another solution
filter(lambda x: x > 0, [1, 2, 3, -3, 6, -1, -3, 1])[1, 2, 3, 6, 1]
But if you really want to "remove the negative signs" from the numbers, that's even easier. For example, to remove the negative sign from -3
, you just negate it and get 3
, right? You can do this in-place, as in your existing code:
for index, item in enumerate(num_list): if item < 0: num_list[index] = -item
… or in a list comprehension, as in arshajii's:
num_list = [-item
if item < 0
else item
for item in num_list
]
And it's even easier with the abs
function, which does exactly that—negates negative numbers, leaves positive and zero alone:
num_list = [abs(item) for item in num_list]
Other the the conventional variable operator non-variable
Try some yoda conditions too =)
>>> [i for i in x if 0 <= i ] [1, 2, 3, 6, 1]
I think an elegant solution can be like this:
import numpy as np x = [1, 2, 3, -3, 6, -1, -3, 1] # raw data x = np.array(x) # convert the python list to a numpy array, to enable matrix operations x = x[x >= 0] # this section `[x >=0]` produces vector of True and False values, of the same length as the list x # as such, this statement `x[x >=0]` produces only the positive values and the zeros print(x)[1 2 3 6 1] # the result
6 days ago Mar 18, 2021 · remove all negative numbers from list python. remove negative values from list python. def remove_negs (num_list): '''remove the negative numbers from the list num_list.''' for item in num_list: if item < 0: num_list.remove (item) print num_list main () how to remove negative numbers from a list in python. , 4 days ago Sep 01, 2021 · remove negative numbers from list python. Hsm. my_positive_list = [number for number in my_list if number >= 0] Add Own solution. Log in, to leave a comment. , Python program to print negative numbers in a list. Given a list of numbers, write a Python program to print all negative numbers in given list. Example: Iterate each element in the list using for loop and check if number is less than 0. If the condition satisfies, then only print the number. , 1 week ago Aug 08, 2022 · Example #1: Print all negative numbers from the given list using for loop Iterate each element in the list using for loop and check if the number is less than 0. If the condition satisfies, then only print the number. Python3. list1 = [11, -21, 0, 45, 66, -93] for num in list1: if num < 0: print(num, end = " ")
def remove_negs(num_list): ''
'Remove the negative numbers from the list num_list.'
''
for item in num_list: if item < 0: num_list.remove(item) print num_list main()
def remove_negs(num_list): ''
'Remove the negative numbers from the list num_list.'
''
for item in num_list: if item < 0: num_list.remove(item) print num_list main()
num_list = [item for item in num_list if item >= 0 ]
num_list[: ] = ...
>>> a = [1, 2, 3, -3, 6, -1, -3, 1] >>> [x for x in a if x >= 0 ][1, 2, 3, 6, 1]
def remove_negs(num_list): r = num_list[: ]
for item in num_list: if item < 0: r.remove(item) print r
No value is returned by this method. The clear() function is used to empty the list().,There is no return value. The list() is emptied using clear() method.,index: There is only one argument, named index, for the pop() method.,Python Design Patterns
Initial List is: ['Javatpoint', 'Python', 'Tutorial', 'List', 'Element', 'Removal']
After using the
function: ['Javatpoint', 'Tutorial', 'List', 'Element', 'Removal']
Initial List is: ['Python', 'Remove', 'Elements', 'List', 'Tutorial']
Element that is popped: Elements
List after deleting the element['Python', 'Remove', 'List', 'Tutorial']
Initial List is: ['Python', 'Remove', 'Elements', 'List', 'Tutorial']
None
[]
The Initial list is['Python', 'Remove', 'Elements', 'List', 'Tutorial', 'Clear', 'Pop', 'Remove', 'Delete']
After removing the first element new list is['Remove', 'Elements', 'List', 'Tutorial', 'Clear', 'Pop', 'Remove', 'Delete']
After removing the last element new list is['Remove', 'Elements', 'List', 'Tutorial', 'Clear', 'Pop', 'Remove']
After removing element from index: 5['Tutorial', 'Clear', 'Pop', 'Remove']
After removing the last 2 elements from the list['Tutorial', 'Clear', 'Remove']
After removing elements present in the range 1: 5['Tutorial']
In Example 1, I’ll explain how to remove negative values from a vector object in R.,In this example, I’ll demonstrate how to delete all rows with negative values in a data frame.,Find Missing Values (6 Examples for Data Frame, Column & Vector),Now, we can remove all rows with NA values from this updated data frame to create another data frame without those rows. For this task, we can apply the na.omit function as shown below:
vec < -c(1, 5, -3, 2, 2, -5, 7) # Create example vector
vec # Print example vector
#[1] 1 5 - 3 2 2 - 5 7
vec_new < -vec[vec >= 0] # Remove negative vector elements
vec_new # Print updated vector
#[1] 1 5 2 2 7
data < -data.frame(x1 = -2: 4, # Create example data frame x2 = 5: -1, x3 = 1: 7) data # Print example data frame
data_new1 < -data # Duplicate data frame
data_new1[data_new1 < 0] < -NA # Replace negative values by NA
data_new1 # Print updated data frame
data_new2 < -na.omit(data_new1) # Remove rows with NA values data_new2 # Print updated data frame