Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Keep only even numbers from a listE02. Transform each item in a listE03. Extract unique elements while keeping orderE

Problem No 1

Keep only even numbers from a list

Easy

6 minute session

Summary

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

Problem statement

Given a list of integers, implement a function keep_even(nums) that returns a new list containing only the even numbers from nums in the same order they appeared. Zero and negative even numbers count as even. If there are no even numbers, return an empty list.

Task

Write a function that filters a list to keep only even numbers while preserving the original order.

Examples

Basic example

Input

keep_even([1, 2, 3, 4, 0, -2])

Output

[2, 4, 0, -2]

Explanation

Only the even numbers (2, 4, 0, -2) are kept in the original order.

Input format

A single list of integers.

Output format

A list of integers containing only the even numbers from the input list, preserving order.

Constraints

The input list length can be from 0 up to a few thousand elements. Elements are integers.

Samples

Sample input 0

[1, 3, 5]

Sample output 0

[]

Explanation 0

No even numbers -> empty list.

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:49 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.