Write a Python Program to Put Even and Odd Numbers in Separate List using For Loop, While Loop, and Functions with practical example.

Before you start, please refer List article to understand everything about Lists.

## Python Program to Put Even and Odd Numbers in Separate List using For Loop

In this python program, we are using For Loop to iterate each and every element in a given List. Inside the loop we are using If statement to check whether the list item is Even or odd. Based on the result, we are appending that item to Even list or Odd list.

# Python Program to Put Even and Odd Numbers in Separate List NumList = [] Even = [] Odd = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) for j in range(Number): if(NumList[j] % 2 == 0): Even.append(NumList[j]) else: Odd.append(NumList[j]) print("Element in Even List is : ", Even) print("Element in Odd List is : ", Odd)

**OUTPUT**

**ANALYSIS**

In this Python Program to separate Even and Odd values in List, User entered items = [22, 33, 44, 55, 77]

**For Loop – First Iteration: **for 0 in range(0, 5)

The condition is True so, compiler will enter into the If Statement

if(NumList[0] % 2 == 0) => if(22 % 2 == 0) – Condition is True

Even.append(NumList[0]) => Even = [22]

**Second Iteration:** for 1 in range(0, 5) – Condition is True

if(NumList[1] % 2 == 0) => if(33 % 2 == 0) – Condition is False

Compiler will enter into the Else block.

Odd.append(NumList[1]) => Odd = [33]

**Third Iteration:** for 2 in range(0, 5) – Condition is True

if(NumList[2] % 2 == 0) => if(44 % 2 == 0) – Condition is True

Even.append(44) => Even = [22, 44]

**Fourth Iteration:** for 3 in range(0, 5) – Condition is True

if(55 % 2 == 0) – Condition is False

Compiler will enter into Else block.

Odd.append(55) => Odd = [33, 55]

**Fifth Iteration:** for 4 in range(0, 5) – Condition is True

if(77 % 2 == 0) – Condition is False

Compiler will enter into Else block.

Odd.append(77) => Odd = [33, 55, 77]

**Sixth Iteration:** for 5 in range(5) – Condition is False

So compiler will exit from For Loop

## Python Program to Put Even and Odd Numbers in Separate List using While loop

This program for placing even numbers in Even List, and odd numbers in Odd List is same as above. We just replaced the For Loop with While loop.

# Python Program to Put Even and Odd Numbers in Separate List NumList = [] Even = [] Odd = [] j = 0 Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) while(j < Number): if(NumList[j] % 2 == 0): Even.append(NumList[j]) else: Odd.append(NumList[j]) j = j + 1 print("Element in Even List is : ", Even) print("Element in Odd List is : ", Odd)

**OUTPUT**

## Python Program to Put Even and Odd Numbers in Separate List using Functions

This program is same as first example. However, we separated the logic using Functions. Remember, instead of writing separate functions for Even and Odd, you can also write single function.

# Python Program to Put Even and Odd Numbers in Separate List def even_numbers(NumList): Even = [] for j in range(Number): if(NumList[j] % 2 == 0): Even.append(NumList[j]) print("Element in Even List is : ", Even) def odd_numbers(NumList): Odd = [] for j in range(Number): if(NumList[j] % 2 != 0): Odd.append(NumList[j]) print("Element in Odd List is : ", Odd) NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) even_numbers(NumList) odd_numbers(NumList)

**OUTPUT**