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
46.8k12 gold badges120 silver badges145 bronze badges
asked Jun 5, 2015 at 21:10
3
  • Don't understand the downvotes, just trying to ask why python disagrees with the assignment Commented Jun 5, 2015 at 21:11
  • This is the solution without lambda: stackoverflow.com/questions/2213923/… Commented 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. Commented Jun 5, 2015 at 21:50

1 Answer 1

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

answered Jun 5, 2015 at 21:20
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.