The goal is to write a function that takes in a list and returns whether the numbers in the list are even resulting in True or False. Ex. [1, 2, 3, 4] ---> [False, True, False, True]
I've written this portion of code:
def even_or_odd(t_f_list):
return = [ x for x in t_f_list if x % 2 == 0]
I know this code would return [2, 4]. How would I make it so it instead returns a true and false like the above example?
Instead of filtering by predicate, you should map it:
def even_or_odd(t_f_list):
return [ x % 2 == 0 for x in t_f_list]
You can also use lambda instead :
l = [1,2,3,4]
map(lambda x: x%2 == 0, l)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With