Q:

Python | Program to Create two lists with EVEN numbers and ODD numbers from a list

belongs to collection: Python List Programs

0

Given a list, and we have to create two lists 1) list with EVEN numbers and 2) list with ODD numbers from given list in Python.

Example:

    Input:
    List1 = [11, 22, 33, 44, 55]

    Output:
    List with EVEN numbers: [22, 44]
    List with ODD NUMBERS: [11, 33, 55]

Logic:

To create lists with EVEN and ODD numbers, we will traverse each element of list1 and append EVEN and ODD numbers in two lists by checking the conditions for EVEN and ODD.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

# declare and assign list1
list1 = [11, 22, 33, 44, 55]

# declare listOdd - to store odd numbers
# declare listEven - to store even numbers
listOdd = []
listEven = []

# check and append odd numbers in listOdd
# and even numbers in listEven
for num in list1:
	if num%2 == 0:
		listEven.append(num)
	else:
		listOdd.append(num) 

# print lists
print "list1:    ", list1 
print "listEven: ", listEven
print "listOdd:  ", listOdd

Output

    list1:     [11, 22, 33, 44, 55]
    listEven:  [22, 44]
    listOdd:   [11, 33, 55]

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Program to print all numbers which are di... >>
<< Python | Program to remove duplicate elements from...