GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Filter the positive numbers from a list - w3resource

Python: Filter the positive numbers from a list

Last update on May 17 2025 05:43:15 (UTC/GMT +8 hours)


Filter Positive Numbers

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

Positive Numbers: Any number above zero is known as a positive number. Positive numbers are written without any sign or a '+' sign in front of them and they are counted up from zero, i.e. 1, + 2, 3, +4 etc.

Pictorial Presentation:

Filter the positive numbers from a list


Sample Solution-1:

Python Code:

# Define a list named "nums" containing a series of integers.
nums = [34, 1, 0, -23, 12, -88]

# Print a message along with the original list of numbers.
print("Original numbers in the list: ", nums)

# Create a new list "new_nums" by using the "filter" function with a lambda function
# that filters out positive numbers from the "nums" list.
new_nums = list(filter(lambda x: x > 0, nums))

# Print a message along with the list of positive numbers from the "nums" list.
print("Positive numbers in the said list: ", new_nums)

Sample Output:

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

Sample Solution-2:

Python Code:

# Define a list named "nums" containing a series of integers.
nums = [34, 1, 0, -23, 12, -88]

# Print a message along with the original list of numbers.
print("Original numbers in the list: ", nums)

# Print a message indicating that positive numbers in the list will be listed.
print("Positive numbers in the said list: ")

# Iterate through each number in the "nums" list.
for pos_nums in nums:
    # Check if the number is greater than 0 (positive).
    if pos_nums > 0:
        # Print positive numbers on the same line with a space separator.
        print(pos_nums, end=" ")

Sample Output:

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

Flowchart:

Flowchart: Filter the positive numbers from a list.


Sample Solution-3:

Python Code:

# Define a list named "nums" containing a series of integers.
nums = [34, 1, 0, -23, 12, -88]

# Print a message along with the original list of numbers.
print("Original numbers in the list: ", nums)

# Create a new list "pos_nums" using a list comprehension to filter positive numbers from "nums."
# Positive numbers are those greater than 0.
pos_nums = [n for n in nums if n > 0]

# Print a message indicating that positive numbers in the list will be listed,
# followed by the positive numbers separated by a space.
print("Positive numbers in the said list: ", *pos_nums)

Sample Output:

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

For more Practice: Solve these Related Problems:

  • Write a Python program to filter negative numbers from a list.
  • Write a Python program to remove zero values from a list.
  • Write a Python program to find all even numbers in a list using the filter function.
  • Write a Python program to count the number of positive numbers in a list.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

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

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.