Lesson guide
What this Python exercise practices
Keep only even numbers from a list is a beginner practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Related public exercises
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.
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.