List and order

Peter Otten __peter__ at
Mon May 15 15:03:09 CEST 2006

Nic wrote:

> I tried to insert and execute the code, but the following error happens:
> Traceback (most recent call last):
>   File "", line 10, in ?
>     edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
> TypeError: <lambda>() takes exactly 2 arguments (1 given)
> Do you know how is it possible to delete it?

Note that 

lambda a, b: ...

takes two arguments while

lambda (a, b): ...

takes one argument which must be a sequence (list, string, generator,...) of
two items.

So the above should probably be 

edges = list(G.edges())
edges.sort(key=lambda (u, v): (ddeg[u], ddeg[v]))
for u, v in edges:
    print ddeg[u], ddeg[v],

Here's how I would do it:

edges = [(ddeg[u], ddeg[v]) for u, v in G.edges()]
for a, b in edges:
    print a, b,

(all untested)


More information about the Python-list mailing list