Odd and even number lists


#1

I am trying to list all odd and even numbers. I have it but it is dropping the 9 in the output.

numbers = [1,2,3,4,5,6,7,8,9]

listEven = []
listOdd = []

for numbers in range(1, len(numbers)):
if numbers % 2 == 0:
listEven.append(numbers)
else:
listOdd.append(numbers)

print(listOdd)
print(listEven)

Output:
[1, 3, 5, 7]
[2, 4, 6, 8]

There should be a 9 in the first list. Please help me figure out what I am doing wrong.

Thanks!


#2

Using an iterator variable that is the same name as the list we are iterating is the first indication of confusion. With the names at one’s disposal, is there no other name?

for number in numbers:

Iterate the terms, not a range.


#3

You’re ignoring what’s in the list, if you put strings in the list instead you’ll find that none of them will show in your output


#4

Thank you! It is now working for me.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.