Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 5

Filter Even Numbers

Easy

8 minute session

Summary

Return a new list containing only the even numbers from the input list, preserving order.

Problem statement

Write a function filter_evens(nums) that takes a list of integers nums and returns a new list containing only the even integers from nums in the same order they appeared. If there are no even numbers, return an empty list.

Task

Practice iterating over a list, applying a condition, and building a new list.

Examples

Mixed numbers

Input

filter_evens([1, 2, 3, 4, 5, 6])

Output

[2, 4, 6]

Explanation

Only even numbers 2, 4, 6 are kept, in the original order.

Input format

A single list of integers, e.g. [0, 1, 2].

Output format

A list of integers containing only even values, e.g. [0, 2].

Constraints

Do not use external libraries. Aim for O(n) time and O(n) output space (new list).

Samples

Sample input 0

filter_evens([0, -2, 7])

Sample output 0

[0, -2]

Explanation 0

0 and -2 are even; 7 is odd and removed.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:47 AM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.