Python bootcamp · Lab 32

Keep the evens

easyList Comprehension10 minLesson: List Comprehension

Read the question, write Python on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

The first line is n. The second line has n integers. Print only the even values, in the same order, space-separated.

A filtered list comprehension is the whole program after you read the list.

Input. Line 1: n. Line 2: n integers.

Output. The even values, space-separated. If none, print a blank line.

Constraints

  • 1 ≤ n ≤ 100

Examples

Example 1
Input
6
1 2 3 4 5 6
Output
2 4 6
Example 2 — No evens — empty output.
Input
3
1 3 5
Output
Hint
  1. [x for x in nums if x % 2 == 0]
  2. print(*evens) — an empty list prints a blank line.
Show correct code

Peek only after you have tried. You can still Check your own version.

n = int(input())
nums = list(map(int, input().split()))
print(*[x for x in nums if x % 2 == 0])
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.