Even numbers from a list
Write a Python function that returns the even numbers of a list.
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
Approach
- A list comprehension in the return statement.
Solution
def evens(numbers): return [n for n in numbers if n % 2 == 0] print(evens([1, 2, 3, 4, 5, 6, 7, 8, 9]))[2, 4, 6, 8]
Learn the concept
New to this topic? Read the lesson, then come back.