by BehindJava
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
- User must enter the number of elements and store it in a variable.
- User must then enter the elements of the list one by one using a for loop and store it in a list.
- Another for loop is used to traverse through the elements of the list.
- The if statement checks if the element is even or odd and appends them to separate lists.
- Both the lists are printed.