Last Updated : 11 Jul, 2025
In this article, we will explore various methods to o print positive numbers in a list. The simplest way to do is by using for loop function.
Using LoopThe most basic method for printing positive numbers is to use a for loop to iterate through the list and check each element.
Python
a = [-10, 15, 0, 20, -5, 30, -2]
# Using a for loop to print positive number
for val in a:
if val > 0:
print(val)
Explanation:
List comprehension provides a compact and Pythonic way to filter positive numbers.
Python
a = [-10, 15, 0, 20, -5, 30, -2]
# List comprehension to filter positive numbers
res = [i for i in a if i > 0]
print(res)
Using filter() Function
The filter() function can be used to filter out positive numbers from the list.
Python
a = [-10, 15, 0, 20, -5, 30, -2]
# Using filter() to get positive numbers
res = filter(lambda x: x > 0, a)
# Convert filter object to list and print
print(list(res))
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4