I'm trying to find duplicates in a list by using a lambda function:
f = lambda z,y: z=[] if z[0]==y[0] and z[1]==y[1]
I have a list like
[['hey','ho'], ['hey','ho'], ['howdy','no']]
and I would like
['hey','ho'], ['howdy','no']]
I get the error:
>>> f = lambda z,y: z=[] if z[0]==y[0] and z[1]==y[1] else z=z
File "<stdin>", line 1
SyntaxError: can't assign to lambda
-
Don't understand the downvotes, just trying to ask why python disagrees with the assignmentFalcata– Falcata2015年06月05日 21:11:56 +00:00Commented Jun 5, 2015 at 21:11
-
This is the solution without lambda: stackoverflow.com/questions/2213923/…fsacer– fsacer2015年06月05日 21:17:00 +00:00Commented Jun 5, 2015 at 21:17
-
@Falcata Downvotes reflect a perceived lack of research, given that you could have answered the question by looking up the correct syntax for both lambda expressions and conditional expressions.chepner– chepner2015年06月05日 21:50:32 +00:00Commented Jun 5, 2015 at 21:50
1 Answer 1
The lambda needs to be an expression that evaluates to some value. It should not be an assignment to a variable. Get rid of the assignments to z.
f = lambda z,y: [] if z[0]==y[0] and z[1]==y[1] else z
or more simply
f = lambda z,y: [] if z==y else z
(Strange variable names, by the way. Why z and y, and why are they backwards?)
answered Jun 5, 2015 at 21:20
John Kugelman
364k70 gold badges555 silver badges600 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py