Python Program to Put Even and Odd Numbers in Separate List

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

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 element in a given List. Inside the Python loop, we are using the If statement to check whether the list item is Even or odd. Based on the result, we are appending that item to the 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)
Python Program to Put Even and Odd Numbers in Separate List 1

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, it enters 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, so it enters 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 and it enters 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, so it enters into Else block.
Odd.append(77) => Odd  = [33, 55, 77]

Sixth Iteration: for 5 in range(5) – Condition is False. So, Python exits from For Loop

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

This program to place even numbers in Even List and odd numbers in Odd List is the 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)
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 11
Please enter the Value of 2 Element : 33
Please enter the Value of 3 Element : 55
Please enter the Value of 4 Element : 4
Element in Even List is :  [4]
Element in Odd List is :  [11, 33, 55]

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

This program to place odd and even numbers in a separate list is the same as the first example. However, we separated the logic using Functions. Remember, instead of writing separate functions for Even and Odd, you can also write a 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)
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 45
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 78
Please enter the Value of 4 Element : 98
Please enter the Value of 5 Element : 22
Element in Even List is :  [56, 78, 98, 22]
Element in Odd List is :  [45]