Python-courses-python-exercises-python-files

Exercise 43

Write a Python algorithm as a function which takes as parameter a list of integers L and which returns the list obtained from L by removing all negative numbers. Example if L = [7, -2, 11, -25, 16, -3, 14], the algorithm returns the list: [7, 11, 16, 14]

Solution

def deleteNegative(L):
for x in L:
if x < 0:
L.remove(x)
return L

L = [7 , -2 , 11 , -25 , 16 , -3 , 14]
print(deleteNegative (L) ) # display [7, 11, 16, 14]
Younes Derfoufi
my-courses.net

Leave a Reply