Falcata
Falcata

Reputation: 707

conditionals in lambda statements

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

Upvotes: 0

Views: 103

Answers (1)

John Kugelman
John Kugelman

Reputation: 361730

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?)

Upvotes: 3

Related Questions