Python Practice › List Programs › Program 6
Separate even and odd numbers
Write a Python program to separate the even and odd numbers of a list.
Input
[5, 8, 3, 12, 7, 10]
Output
Even: [8, 12, 10] Odd: [5, 3, 7]
Approach
- Two comprehensions with opposite conditions.
Solution
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.