Using a for loop in Python write code to iterate through every number in the list below to separate out even and odd numbers. Then return the number of odd and even numbers in the list.
my_numbers = [12, 21, 33, 14, 57, 6, 17, 10, 11, 28, 3, 32, 2, 4]
This is what I have so far:
my_numbers = [12, 21, 33, 14, 57, 6, 17, 10, 11, 28, 3, 32, 2, 4]
splitevenodd(A)
def splitevenodd(A):
evenlist = []
oddlist = []
for i in my_numbers:
if (i % 2 == 0):
evenlist.append(i)
else:
oddlist.append(i)
print("Even list:", evenlist)
print("Odd list:", oddlist)
my_evennumbers = ["Even list:"]
my_oddnumbers = ["Odd list:"]
print("The total count of numbers in my list is", len(my_numbers))
##counting the even numbers
#print("The count of even numbers in my list is", len(my_evennumbers))
##counting the odd numbers
#print("The count of odd numbers in my list is", len(my_oddnumbers))
I am not sure why this is not working. I am trying to get it to print how many numbers are in each, the even and the odd list. I think I am defining my_evennnumbers wrong but I am not sure how to define it in a way where it knows what I am asking it to count.
I would use list comprehension for this one. This lets you assign the lists directly in a single line. Then you can just calculate the counts with only iterating over the list one.