# Split a list into even and odd numbers

Split a list into even and odd numbers
0

I’m having trouble trying to split a list into even and odd numbers with the variables odd and even representing their respective numbers.

The professor noted that this line of code:

``````odd, even = foo([1,2,3,4,5,6], lambda x : x % 2 == 0)
``````

Should split the numbers into odd and even. How do I do something like this? I know how to filter between odd and even numbers, but I’m unsure of how to set two variables in one line equal to their respective parts.
In your example `foo` is a function, and returns a pair of variables. For example:

``````def foo():
a = 1
b = 2
return (a, b)

x, y = foo()  # x is now '1', and y is now '2'
``````

So you need to create a function that iterates over the input list, and assigns the elements to either an `odd` list or an `even` list. Then return both of these lists, as the above example.
For Reference use the below links

I’m having trouble trying to split a list into even and odd numbers with the variables odd and even representing their respective numbers.

The professor noted that this line of code:

``````odd, even = foo([1,2,3,4,5,6], lambda x : x % 2 == 0)
``````

Should split the numbers into odd and even. How do I do something like this? I know how to filter between odd and even numbers, but I’m unsure of how to set two variables in one line equal to their respective parts.
In your example `foo` is a function, and returns a pair of variables. For example:

``````def foo():
a = 1
b = 2
return (a, b)

x, y = foo()  # x is now '1', and y is now '2'
``````

So you need to create a function that iterates over the input list, and assigns the elements to either an `odd` list or an `even` list.

I moved your question out of the other thread and into a new topic, because you were asking a new question and not answering the original poster of the other thread. Always create a new topic for new questions.

Thank you.

Well, i just start tinkering with Python so I’ll try this one!

This is what i came up with ( a bit verbose but clear i think):

``````def splitOddEven(number_list):
"""it separate a list of numbers into odd and even"""
odd_list = [];
even_list = [];
while(number_list):
current_number = number_list.pop();
if(current_number % 2 == 0):
even_list.append(current_number)
else:
odd_list.append(current_number)

return (even_list, odd_list)

test_list = [2,3,1,5,78,32,121,12]
even, odd = splitOddEven(test_list[:])

print("These are the even numbers of your list:" + str(even))
print("\nThese are the odd numbers of your list: " + str(odd))
``````

I’m unsure of how to set two variables in one line equal to their respective parts.

looks like is just a positional matter^^

There’s a number of ways to do this. You could create a function that specifically splits a list into even and odd sets. However, the way your professor set up this code:

``````odd, even = foo([1,2,3,4,5,6], lambda x : x % 2 == 0)
``````

Makes me believe they are attempting to get you to think a little more broadly by creating a function that accepts a list and a callback function (a simple True or False lambda function in this case) as arguments and returns the True/False lists.

``````splitter_function = lambda x: x % 2 == 0
# This has assigned the lambda function to the variable splitter_function so we can test it
# splitter_function accepts a number as an argument and returns True or False
# depending on if the number is even (True) or odd (False)
print(splitter_function(5))
# False
print(splitter_function(4))
# True
``````

Okay, so knowing that, we need a function `foo()` that accepts a list of numbers and applies a True/False callback function to each number then appends them to an appropriate list for return.

``````def foo(list_of_numbers, splitter_function):
"""Function that accepts a list of numbers and splits them into
truthy and falsy lists based upon the callback function argument"""
f = []  # Falsy list
t = []  # Truthy list
for number in list_of_numbers:
if splitter_function(number):
t.append(number)
# If splitter_function returns True --> append number to list t
else:
f.append(number)
# Else if splitter_function returns False --> append number to list f
return (f, t)  # Return the lists, Falsy (odd potentially) first, then Truthy (even potentially)
``````

Now to use it:

``````print(foo([1, 2, 3, 4, 5, 6], lambda x: x % 2 == 0))
# ([1, 3, 5], [2, 4, 6])

odd, even = foo([1, 2, 3, 4, 5, 6], lambda x: x % 2 == 0)
print(odd)
#[1, 3, 5]
print(even)
# [2, 4, 6]

# This can also be used to check if numbers are divisible by numbers other than 2 as well
not_divisible, divisible = foo([1, 2, 3, 4, 5, 6], lambda x: x % 3 == 0)
print(not_divisible)
# [1, 2, 4, 5]
print(divisible)
# [3, 6]
``````

Hope that helps.

1 Like