Write a Python lambda function which takes a list called my_list and returns a list which includes only even numbers in the same order.
My solution:
f = list(filter(lambda x: (x % 2 == 0), my_list))
print(f)
I'm having issues because I'm doing a homework problem online and it runs various test cases on it and it says my_list isn't defined. Which I understand why that is, because it isn't... but it seems like the question is looking for a broad function which I don't know how to do in this case.
2 questions:
- Does my solution look correct?
- If so, how do I tailor this so that the homework will accept various inputs?
my_lista list of arbitrary integers as test data, e.g.my_list = [3, 7, 4, 1, 2]f = lambda lst: [x for x in lst if x % 2 == 0]You can call it on any list of integers.