Thursday, December 26, 2024
Google search engine
HomeLanguagesPython Programs to Split Even and Odd Numbers in Separate List

Python Programs to Split Even and Odd Numbers in Separate List

Python program to separate even and odd numbers of a list; In this tutorial, you will learn how to separate odd and even numbers from list in python.

Python program to find or separate odd and even numbers from a 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.

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

  • First of all, declare a number list, even number list and odd number list in python.
  • 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.
  • Store elements in list using list.appned() function.
  • Iterate for loop and find even and odd number using if else statement
  • Print both lists.
# 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]

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

  • First of all, declare a number list, even number list and odd number list in python.
  • 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.
  • Store elements in list using list.appned() function.
  • Iterate while loop and find even and odd number using if-else statement
  • Print both lists.
# 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]

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

  • First of all, declare a number list.
  • 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 List Programs

  1. Python Print List Elements in Different Way
  2. How to Input List From User in Python
  3. Python Add and Remove Elements From List
  4. Python: Add/Insert Element at Specified Index in List
  5. Python Program to Remove ith/Nth Occurrence of Given Word in List
  6. Python Program to Sort List in Ascending and Descending Order
  7. Python to Find the Differences of Two Lists
  8. Python to Find Minimum and Maximum Elements of List
  9. Python Program to Swap Two Elements in a List
  10. Python Program to Reverse List
  11. How To Select Random Item From A List In Python
RELATED ARTICLES

Most Popular

Recent Comments