How to Select Even and Odd Elements in a List in Python


In this example, we will learn how to pick out the odd and even numbers in a list in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = input("Input a list of numbers separed by SPACE: ")
a = n.split()
odd = []
even = []
for i in a:
    b = int(i)
    if b % 2 == 0:
        even.append(b)
    else:
        odd.append(b)
print("Odd number list: ", odd)
print("Even number list: ", even)

Output:

Input a list of numbers separed by SPACE: 2 5 8 6 7 9
Odd number list: [5, 7, 9]
Even number list: [2, 8, 6]

Input a list of numbers separed by SPACE: 13 15 12 19 16 13 12
Odd number list:  [13, 15, 19, 13]
Even number list: [12, 16, 12] 
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement