w3resource
Python Exercises

Python: Filter the positive numbers from a list

Python Basic: Exercise-114 with Solution

Write a Python program to filter the positive numbers from a list.

Pictorial Presentation:

Filter the positive numbers from a list

Sample Solution:-

Python Code:

nums = [34, 1, 0, -23]
print("Original numbers in the list: ",nums)
new_nums = list(filter(lambda x: x >0, nums))
print("Positive numbers in the list: ",new_nums)

Sample Output:

Original numbers in the list:  [34, 1, 0, -23]                                                                
Positive numbers in the list:  [34, 1]

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to input a number, if it is not a number generate an error message.
Next: Write a Python program to compute the product of a list of integers (without using for loop).

What is the difficulty level of this exercise?