Hello everyone! Welcome back to programminginpython.com Here in the post am going to add one program which covers the python data-type `list`

. Here I will separate all the even and odd numbers from a list to two different lists.

#### Separate even and odd numbers in a list – Code Visualization

#### Task:

Separate even an odd numbers from a list and add them to new lists.

#### Approach:

- Read input number asking for length of the list using
`input()`

- Initialise an empty list
`numbers = []`

- Read each number using a
`for loop`

- In the for loop append each number to the list
`numbers`

- Create another two empty lists
`even_lst = []`

and`odd_lst = []`

- Now run another for loop to check the numbers in the list are divided by 2 or not
- If the numbers are divided by 2, append those elements to
`even_lst`

else append those to`odd_lst`

- Print both the
`even_lst`

and`odd_lst`

#### Program:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
__author__ = 'Avinash' numbers = [] n = int(input("Enter number of elements: \t")) for i in range(1, n+1): allElements = int(input("Enter element:")) numbers.append(allElements) even_lst = [] odd_lst = [] for j in numbers: if j % 2 == 0: even_lst.append(j) else: odd_lst.append(j) print("Even numbers list \t", even_lst) print("Odd numbers list \t", odd_lst) |

How about this approach:

def split_even_odd(A):

even=[]

pos=0

while True:

n=len(A)

if n>0 and pos<n:

if A[pos] % 2==0:

even.append(A[pos])

A.remove(A[pos])

else:

pos=pos+1

else:

break

return even, A