Project Description
Your task for today is to write a Python program that takes a list of numbers and removes all the negative ones. The goal is to keep only numbers that are greater than or equal to zero.
This is a great exercise in list filtering, which is something you’ll often need in real-world projects like:
Cleaning data for analysis
Preparing inputs for machine learning
Validating form entries or user input
You’ll start with a predefined list of integers — some positive, some negative, and maybe even zero. Store that list in a variable like so:
numbers = [3, -1, 7, -5, 0, 8, -2]
Your program should create a new list that includes only the non-negative numbers and then print it.
Expected Output
If the list is:
[3, -1, 7, -5, 0, 8, -2]
Then your program should print:
💡 Hint
Click the Show Hint button below if you’re not sure how to get started. It will guide you with the first line of code and explain the logic.
𝌣 Solution
Click the Show Solution button below to see two different ways to solve this challenge — one using a loop and one using list comprehension.
🔒 This solution is available to paid subscribers only.
🚀 Want to keep leveling up?
Browse 200+ projects at dailypythonprojects.substack.com/archive and unlock all solutions by subscribing.
Build real Python skills daily and transform how you learn — one project at a time.
```
def pos_only(numlist:list) -> list:
'''
filter all negative values from a list
[3, -1, 7, -5, 0, 8, -2] -> [3, 7, 0, 8]
'''
return [x for x in numlist if x >= 0]
```
pos=[num for num in numbers if num>0]