Python Program to Split Even and Odd Numbers in List

Python program to separate or split even and odd numbers; Through this tutorial, i am going to show you how to split or separate even and odd numbers in list in python.

In this tutorial, i will write python program to split the even and odd elements or numbers into two different lists using for loop, while loop and function.

Python Program to Split Even and Odd Numbers in List

  • Python Program to Split Even and Odd Numbers in Separate List using For Loop.
  • Python Program to Split Even and Odd Numbers in Separate List using While Loop.
  • Python Program to Split Even and Odd Numbers in Separate List using Functions.

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

  • To declare numbers list in python program.
  • Get input numbers limit of the list from the user.
  • Iterate for loop with input() function to take a single input number from the user.
  • Store elements in list using list.appned() function.
  • Iterate for loop and find even and odd number using if else statement.
  • Print split even and odd numbers.
# Python Program to Put Even and Odd Numbers in Separate List
NumList = []
Even = []
Odd = []
Number = int(input("How many elements in list :- "))
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("\n Element in Even List is : ", Even)
print("\n Element in Odd List is : ", Odd)

After executing a program, the output will be:

How many elements in list :-  6

Please enter the Value of 1 Element :-  5
Please enter the Value of 2 Element :-  8
Please enter the Value of 3 Element :-  69
Please enter the Value of 4 Element :-  78
Please enter the Value of 5 Element :-  45
Please enter the Value of 6 Element :-  25

 Element in Even List is :  [8, 78]

 Element in Odd List is :  [5, 69, 45, 25]

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

  • To define number list in python.
  • Get input numbers limit of the list from the user.
  • Iterate for loop with input() function to take a single input number from the user.
  • Store elements in list using list.appned() function.
  • Iterate while loop and find even and odd number using if-else statement.
  • Print split even and odd numbers.
# Python Program to Put Even and Odd Numbers in Separate List
#using while loop
NumList = []
Even = []
Odd = []
j = 0
Number = int(input("How many elements in list :- "))
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("\n Element in Even List is : ", Even)
print("\n Element in Odd List is : ", Odd)

After executing the program, the output will be:

How many elements in list :-  6

Please enter the Value of 1 Element :-  54
Please enter the Value of 2 Element :-  68
Please enter the Value of 3 Element :-  84
Please enter the Value of 4 Element :-  25
Please enter the Value of 5 Element :-  63
Please enter the Value of 6 Element :-  47

 Element in Even List is :  [54, 68, 84]

 Element in Odd List is :  [25, 63, 47]

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

  • To define number list in python program.
  • Define a function to find odd number list from given list.
  • Define a function to find odd number list from given list.
  • Take the Input limit of the list from the user.
  • Iterate for loop with input() function to take a single input number from the user.
  • Call even and odd number function.
# Python Program to Put Even and Odd Numbers in Separate List
# using functions
def evenNum(NumList):
    Even = []
    for j in range(Number):
        if(NumList[j] % 2 == 0):
            Even.append(NumList[j])
    print("Element in Even List is : ", Even)
def oddNum(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("How many elements in list :- "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element :- " %i))
    NumList.append(value)
evenNum(NumList)
oddNum(NumList)

After executing the program, the output will be:

How many elements in list :-  6

Please enter the Value of 1 Element :-  99
Please enter the Value of 2 Element :-  88
Please enter the Value of 3 Element :-  45
Please enter the Value of 4 Element :-  63
Please enter the Value of 5 Element :-  25
Please enter the Value of 6 Element :-  44

Element in Even List is :  [88, 44]
Element in Odd List is :  [99, 45, 63, 25]

Recommended Python Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *