Order a list of dictionary in Python with numeric keys

Viewed 458

I have a list of arrays that contains a dictionary in python. Here there is my example:

[[(0, 1), 'up'], [(1, 2), 'right'], [(3, 2), None], [(0, 0), 'up'], [(3, 0), 'left'], [(3, 1), None], [(2, 1), 'up'], [(2, 0), 'left'], [(2, 2), 'right'], [(1, 0), 'left'], [(0, 2), 'right']]

What I need to do is to order it base on the key (x, y) first on x and after on y: is it possible?

Here there is what I want as output:

[[(0, 0), 'up'], [(0, 1), 'up'], [(0, 2), 'right'], [(1, 0), 'left'],[(1, 2), 'right'], [(2, 0), 'left'], ....]

I have tried using that code:

s = sorted(s, key = lambda x: (x[0][0][0], x[0][0][1]))

But it returns:

TypeError: 'int' object has no attribute '__getitem__'
1 Answers
Related