Odd and Even numbers in a List

Python tutorial to get odd and even numbers from a list.
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = []
list3 = []
for i in list1:
    if i % 2 != 0:
       list2.append(i)
    elif i % 2 == 0:
       list3.append(i)
print(list2)
print(list3)
#Output
[1, 3, 5, 7, 9]
[2, 4, 6, 8]

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply

Do NOT follow this link or you will be banned from the site!