0

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
vaultah
  • 44,105
  • 12
  • 114
  • 143
Falcata
  • 679
  • 1
  • 15
  • 23
  • Don't understand the downvotes, just trying to ask why python disagrees with the assignment – Falcata Jun 05 '15 at 21:11
  • This is the solution without lambda: http://stackoverflow.com/questions/2213923/python-removing-duplicates-from-a-list-of-lists – fsacer Jun 05 '15 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 Jun 05 '15 at 21:50

1 Answers1

3

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

John Kugelman
  • 349,597
  • 67
  • 533
  • 578