0
0
PythonProgramBeginner · 2 min read

Python Program to Separate Even and Odd Numbers from List

You can separate even and odd numbers from a list in Python by using list comprehensions like even = [x for x in numbers if x % 2 == 0] and odd = [x for x in numbers if x % 2 != 0].
📋

Examples

Input[1, 2, 3, 4, 5]
OutputEven numbers: [2, 4], Odd numbers: [1, 3, 5]
Input[10, 15, 20, 25, 30]
OutputEven numbers: [10, 20, 30], Odd numbers: [15, 25]
Input[]
OutputEven numbers: [], Odd numbers: []
🧠

How to Think About It

To separate even and odd numbers, look at each number in the list and check if it divides evenly by 2 using the % operator. If the remainder is zero, it is even; otherwise, it is odd. Collect these numbers into two separate lists.
📐

Algorithm

1
Get the input list of numbers.
2
Create an empty list for even numbers.
3
Create an empty list for odd numbers.
4
For each number in the input list, check if it is divisible by 2.
5
If yes, add it to the even list; if no, add it to the odd list.
6
Return or print both the even and odd lists.
💻

Code

python
numbers = [1, 2, 3, 4, 5]
even = [x for x in numbers if x % 2 == 0]
odd = [x for x in numbers if x % 2 != 0]
print("Even numbers:", even)
print("Odd numbers:", odd)
Output
Even numbers: [2, 4] Odd numbers: [1, 3, 5]
🔍

Dry Run

Let's trace the list [1, 2, 3, 4, 5] through the code.

1

Start with the list

numbers = [1, 2, 3, 4, 5]

2

Check each number for evenness

1 % 2 = 1 (odd), 2 % 2 = 0 (even), 3 % 2 = 1 (odd), 4 % 2 = 0 (even), 5 % 2 = 1 (odd)

3

Build even and odd lists

even = [2, 4], odd = [1, 3, 5]

4

Print results

Even numbers: [2, 4] Odd numbers: [1, 3, 5]

NumberNumber % 2Even or Odd
11Odd
20Even
31Odd
40Even
51Odd
💡

Why This Works

Step 1: Check remainder with modulo

Using % 2 gives the remainder when dividing by 2, which tells if a number is even or odd.

Step 2: Separate numbers into lists

Numbers with remainder 0 go into the even list; others go into the odd list.

Step 3: Use list comprehensions

List comprehensions make it easy and concise to filter numbers based on the condition.

🔄

Alternative Approaches

Using for loop and append
python
numbers = [1, 2, 3, 4, 5]
even = []
odd = []
for num in numbers:
    if num % 2 == 0:
        even.append(num)
    else:
        odd.append(num)
print("Even numbers:", even)
print("Odd numbers:", odd)
This approach is more explicit and easier to understand for beginners but longer than list comprehensions.
Using filter and lambda
python
numbers = [1, 2, 3, 4, 5]
even = list(filter(lambda x: x % 2 == 0, numbers))
odd = list(filter(lambda x: x % 2 != 0, numbers))
print("Even numbers:", even)
print("Odd numbers:", odd)
This uses functional programming style but can be less readable for beginners.

Complexity: O(n) time, O(n) space

Time Complexity

The program checks each number once, so time grows linearly with the list size.

Space Complexity

Two new lists are created to store even and odd numbers, so space also grows linearly.

Which Approach is Fastest?

List comprehensions and filter with lambda have similar speed; for loop is slightly slower but clearer for beginners.

ApproachTimeSpaceBest For
List ComprehensionO(n)O(n)Concise and fast code
For Loop with AppendO(n)O(n)Clear and beginner-friendly
Filter with LambdaO(n)O(n)Functional programming style
💡
Use list comprehensions for a clean and fast way to separate even and odd numbers.
⚠️
Forgetting to use % 2 to check evenness and instead using incorrect conditions.