number = [1,2,-5,8,-9] max = number[0] for num in number: if max < 0: print(max) max+=1 |
3 answers
It is possible so:
number = [1,2,-5,8,-9] lst_number = [x for x in number if x < 0] print(lst_number) If the list is not needed separately, and only its output is needed, you can do this:
print([x for x in number if x < 0]) If you just need element-wise output of negative elements, you can:
number = [1,2,-5,8,-9] for num in number: if num < 0: print(num) |
filter(lambda x: x<0, number) |
number = [1,2,-5,8,-9] max = [] for num in number: if num < 0: print(num) max.append(num)) - max can not be used if you do not need to store negative elements in the array - ExUser
- Try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky β¦
|