Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Filter Even Numbers

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

Python practice8 minLoops & IterationBeginnerLast updated March 15, 2026

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]

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 1

Input

filter_evens([0, -2, 7])

Output

[0, -2]

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