Even numbers from a list

EasyFunction Programs · 11 of 39

Write a Python function that returns the even numbers of a list.

Advertisement
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Output
[2, 4, 6, 8]

Approach

  1. A list comprehension in the return statement.

Solution

Python
def evens(numbers):    return [n for n in numbers if n % 2 == 0] print(evens([1, 2, 3, 4, 5, 6, 7, 8, 9]))
Output
[2, 4, 6, 8]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs