Python Forum
Issues with Lambda Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with Lambda Function
#1
Hi,
I am trying to wrap my head around lambda but I am running into issues.

The homework question is: Write a lambda function that takes a list my_list and returns a list which includes only even numbers

my_list = [1, 5, 4, 6, 8, 11, 3, 12]
test = lambda my_list: my_list % 2 == 0


print test(my_list)
I feel like that should do it but I get an error when I try to run it. Not sure what is wrong with my logic.
Reply
#2
you need list comprehension in the lambda
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The Problem is that any numerical operand is only defined for values (int, double, long, etc.) not for lists.
the first solution that pops into my mind is:
my_list = [1,5,4,6,8,11,3,12]
test = lambda mylist: [el for el in mylist if el % 2 == 0]

print(test(my_list))
what you are doing is to get every element in the list, check if it is even and pack all even elements into a new list.
If the amount of elements becomes too high use numpy instead, there you can also use masks. But for small lists this would be a solution. but since you are constructing a new list you could use the list comprehension without the explizit function and lambda, but that depends on what you want to do with the lambda :)
Reply
#4
(May-22-2018, 03:51 PM)buran Wrote: you need list comprehension in the lambda

The list comp does help but it returns boolean expression unless I did it wrong?

my_list = [1, 5, 4, 6, 8, 11, 3, 12]
test = lambda my_list: [my_list % 2 == 0 for my_list in my_list]


print (test(my_list))
Reply
#5
@ThiefOfTime already post the right solution
My code is the same
test = lambda my_list: [x for x in my_list if not x%2]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(May-22-2018, 03:58 PM)ThiefOfTime Wrote: The Problem is that any numerical operand is only defined for values (int, double, long, etc.) not for lists.
the first solution that pops into my mind is:
my_list = [1,5,4,6,8,11,3,12]
test = lambda mylist: [el for el in mylist if el % 2 == 0]

print(test(my_list))
what you are doing is to get every element in the list, check if it is even and pack all even elements into a new list.
If the amount of elements becomes too high use numpy instead, there you can also use masks. But for small lists this would be a solution. but since you are constructing a new list you could use the list comprehension without the explizit function and lambda, but that depends on what you want to do with the lambda :)

Yup this totally solves it. I have to spend some time to understand why yours works your way and mine at the bottom creates a boolean. The tip on the numerical operand piece was super helpful. I totally didnt know that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  having issues with the int() function megu 2 1,670 Sep-23-2020, 04:28 PM
Last Post: perfringo
  Lambda function not return value mbilalshafiq 4 3,297 Jul-04-2020, 07:47 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020