;

Python Program to Put Even and Odd Numbers in a Separate List


Tutorialsrack 02/05/2020 Python

In this python program, we will learn how to put even and odd numbers in a separate list.

Here is the source code of the program to put even and odd numbers in a separate list.

Program 1: Python Program to Put Even and Odd Numbers in a Separate List Using For Loop

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

NumList = []
Even = []
Odd = []

# Take the Input From the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)
    
# Print the List
print("\nOriginal List: ",NumList)        

for j in range(Number):
    if(NumList[j] % 2 == 0):
        Even.append(NumList[j])
    else:
        Odd.append(NumList[j])

print("\nElement in Even List is: ", Even)
print("Element in Odd List is: ", Odd)
Output

Enter the Total Number of List Elements: 8

Enter the Value of 1 Element: 445

Enter the Value of 2 Element: 136

Enter the Value of 3 Element: 423

Enter the Value of 4 Element: 556

Enter the Value of 5 Element: 452

Enter the Value of 6 Element: 895

Enter the Value of 7 Element: 445

Enter the Value of 8 Element: 332

 

Original List:  [445, 136, 423, 556, 452, 895, 445, 332]

 

Element in Even List is:  [136, 556, 452, 332]

Element in Odd List is:  [445, 423, 895, 445]

Program 2: Python Program to Put Even and Odd Numbers in a Separate List Using While Loop

Python Program to Put Even and Odd Numbers in a Separate List Using While Loop
# Python Program to Put Even and Odd Numbers in a Separate List Using While Loop

NumList = []
Even = []
Odd = []
j = 0

# Take the Input from the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)
    
# Print the List
print("\nOriginal List: ",NumList)   

while(j < Number):
    if(NumList[j] % 2 == 0):
        Even.append(NumList[j])
    else:
        Odd.append(NumList[j])
    j = j + 1

print("\nElement in Even List is: ", Even)
print("Element in Odd List is: ", Odd)
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 45

Enter the Value of 2 Element: 65

Enter the Value of 3 Element: 152

Enter the Value of 4 Element: 852

Enter the Value of 5 Element: 789

Enter the Value of 6 Element: 32

 

Original List:  [45, 65, 152, 852, 789, 32]

 

Element in Even List is:  [152, 852, 32]

Element in Odd List is:  [45, 65, 789]


Related Posts



Comments

Recent Posts
Tags