by BehindJava

Write a program to Put Even and Odd elements in a List into Two Different Lists in Python

Home » python » Write a program to Put Even and Odd elements in a List into Two Different Lists in Python

In this tutorial we are going to learn about program to Put Even and Odd elements in a List into Two Different Lists in Python.

Python Program to Put Even and Odd elements in a List into Two Different Lists

a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
    b=int(input("Enter element:"))
    a.append(b)
even=[]
odd=[]
for j in a:
    if(j%2==0):
        even.append(j)
    else:
        odd.append(j)
print("The even list",even)
print("The odd list",odd)

Explanation

  1. User must enter the number of elements and store it in a variable.
  2. User must then enter the elements of the list one by one using a for loop and store it in a list.
  3. Another for loop is used to traverse through the elements of the list.
  4. The if statement checks if the element is even or odd and appends them to separate lists.
  5. Both the lists are printed.