04.06 · Transform and filter values / Practice · 13 min
Keep positive numbers and matching words
A useful filter describes exactly which original items belong in the result.
Activity 6 of 6 in Transform and filter values
Python 3: Lists / 04.06
Practice list filtering
Not started
Use the three original filter exercises with explicit keep rules.
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Your task: three keep rules
From numbers [-10, 15, -20, 30, -5], use filter to collect values greater than 0 into positives. Expect [15, 30]; zero would not count as positive.
From animals ["cat", "dog", "elephant", "giraffe"], keep words beginning with lowercase e in starts_with_e. str.startswith checks the prefix, so expect ["elephant"].
From words ["python", "is", "fun", "and", "challenging"], keep words with length at least 5 in long_words. Expect ["python", "challenging"]. Print positives, starts_with_e and long_words, in that order. Preserve all three source lists.
Know what passes
Store actual lists by collecting the filter iterators. Checks compare output and named lists for the starting inputs, not the technique used. After passing, add 0, a five-character word and an uppercase E word. Predict the strict-positive, inclusive-length and case-sensitive results. Restore the inputs for the fixed checks.
Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)4 steps · Saved work · Instant feedback
YOUR TURN
Apply three clear keep rules
Collect positives greater than zero, animals starting with lowercase e, and words of length at least five. Preserve the inputs and print the three lists in order.
[15, 30] ['elephant'] ['python', 'challenging']
Keep the starting inputs. Print these lines in this order, without extra output.
- Store the requested numbers
- Store the requested animals
- Store the requested words
- Store the requested positives
- Store the requested starts with e
- Store the requested long words
- Output 1 matches the requested result
- Output 2 matches the requested result
- Output 3 matches the requested result
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Keep when the answer is true
Write number > 0, word.startswith("e") and len(word) >= 5 as the three predicate expressions.
Collect the iterator
Wrap each filter call in list(...) so the named result is a list rather than an unconsumed iterator.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
Filtering keeps the original matching values in order. A five-character word belongs in an at-least-five result; zero does not belong in a positive-only result.
Walk through the fix.
Choose Next to inspect one change at a time. Highlighted lines show what to notice. Your editor stays unchanged unless you choose to load a step.
Step 1 of 2
Read the original inputs
Identify the original values before choosing the transformation or keep rule.
numbers = [-10, 15, -20, 30, -5]Expected output for this step
(No output)
Apply the function and inspect each result
Filtering keeps the original matching values in order. A five-character word belongs in an at-least-five result; zero does not belong in a positive-only result.
numbers = [-10, 15, -20, 30, -5]
animals = ["cat", "dog", "elephant", "giraffe"]
words = ["python", "is", "fun", "and", "challenging"]
positives = list(filter(lambda number: number > 0, numbers))
starts_with_e = list(filter(lambda word: word.startswith("e"), animals))
long_words = list(filter(lambda word: len(word) >= 5, words))
print(positives)
print(starts_with_e)
print(long_words)Expected output for this step
[15, 30] ['elephant'] ['python', 'challenging']
See the complete solution
numbers = [-10, 15, -20, 30, -5]
animals = ["cat", "dog", "elephant", "giraffe"]
words = ["python", "is", "fun", "and", "challenging"]
positives = list(filter(lambda number: number > 0, numbers))
starts_with_e = list(filter(lambda word: word.startswith("e"), animals))
long_words = list(filter(lambda word: len(word) >= 5, words))
print(positives)
print(starts_with_e)
print(long_words)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)Continue learning

7 days freeTry The Code Zone
THE CODE ZONE COMMUNITY
Your next step is easier together.
Try the community free for 7 days.
No charge during your trial. Choose your plan on Skool.
- Ask questions and get support in the community
- Watch every course video walkthrough
- Find a study buddy in the 24/7 study room
Make the most of your trial with Premium.
Choose Premium on Skool to try Thursday live help sessions. Bring your questions, work through code, and get unstuck together.
Scheduled Thursdays in Eastern Time. Check the Skool calendar for your local date and time.
Have a website account? Sign in