thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

Python 3: Lists / 04.06

Practice list filtering

Not started

Use the three original filter exercises with explicit keep rules.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob checks -10, 15, -20, 30 and -5 against greater than zero and keeps only 15 and 30.
Rob’s comic strip · Transform and filter values

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.

Expected output
[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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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

ONE STEP FORWARD

Each predicate answers one precise question. Test excluded items as carefully as included ones.