0

trying to swap two elements of a list by using lambda expression, but it is not working.
can someone tell me why it doesn't work?

>>> swap = lambda lst, a, b: lst[a], lst[b] = lst[b], lst[a]
  File "<stdin>", line 1
SyntaxError: can't assign to lambda
  • 7
    A lambda only supports [expressions](https://docs.python.org/3/reference/expressions.html), it does not support [statements](https://docs.python.org/3/reference/simple_stmts.html). – Klaus D. Jan 01 '21 at 13:50
  • [Why can’t lambda expressions contain statements?](https://docs.python.org/3/faq/design.html#why-can-t-lambda-expressions-contain-statements). [Lambda Expressions](https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions) , – wwii Jan 01 '21 at 13:58
  • 1
    Often if you search SO with the Exception's error message you'll find what you need. – wwii Jan 01 '21 at 14:05

0 Answers0