Python Practice › List Programs › Program 6

Separate even and odd numbers

EasyList Programs · 6 of 100

Write a Python program to separate the even and odd numbers of a list.

Advertisement
Input
[5, 8, 3, 12, 7, 10]
Output
Even: [8, 12, 10]
Odd: [5, 3, 7]

Approach

  1. Two comprehensions with opposite conditions.

Solution

Python
numbers = [5, 8, 3, 12, 7, 10]evens = [n for n in numbers if n % 2 == 0]odds = [n for n in numbers if n % 2 != 0]print("Even:", evens)print("Odd:", odds)
Output
Even: [8, 12, 10]
Odd: [5, 3, 7]

Learn the concept

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

More list programs

← All 100 List Programs

Leave a Comment