Python lambda does not accept tuple argument

I am running Eclipse SDK v3.6 with PyDev v2.6 plugin on two PC, with Linux and Windows.

I would like to pass a tuple as an argument, like:

foo = lambda (x,y): (y,x)
print (foo((1,2)))

This works on Linux and gives the correct result:

> (2,1)

On Windows it rises an error:

foo = lambda (x,y): (y,x)
             ^
SyntaxError: invalid syntax

How to solve the problem?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You are probably running Python 3.x on Windows, and Python 2.x on Linux. The ability to unpack tuple parameters was removed in Python 3: See PEP 3113.

You can manually unpack the tuple instead, which would work on both Python 2.x and 3.x:

foo = lambda xy: (xy[1],xy[0])

Or:

def foo(xy):
    x,y = xy
    return (y,x)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x