How to sort the list of tuples by their first element?

I have a list of tuples:

self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.) print self.gridKeys 

self.gridKeys:

 [(7, 3), (6, 9), (0, 7), (1, 6), (3, 7), (2, 5), (8, 5), (5, 8), (4, 0), (9, 0), (6, 7), (5, 5), (7, 6), (0, 4), (1, 1), (3, 2), (2, 6), (8, 2), (4, 5), (9, 3), (6, 0), (7, 5), (0, 1), (3, 1), (9, 9), (7, 8), (2, 1), (8, 9), (9, 4), (5, 1), (7, 2), (1, 5), (3, 6), (2, 2), (8, 6), (4, 1), (9, 7), (6, 4), (5, 4), (7, 1), (0, 5), (1, 0), (0, 8), (3, 5), (2, 7), (8, 3), (4, 6), (9, 2), (6, 1), (5, 7), (7, 4), (0, 2), (1, 3), (4, 8), (3, 0), (2, 8), (9, 8), (8, 0), (6, 2), (5, 0), (1, 4), (3, 9), (2, 3), (1, 9), (8, 7), (4, 2), (9, 6), (6, 5), (5, 3), (7, 0), (6, 8), (0, 6), (1, 7), (0, 9), (3, 4), (2, 4), (8, 4), (5, 9), (4, 7), (9, 1), (6, 6), (5, 6), (7, 7), (0, 3), (1, 2), (4, 9), (3, 3), (2, 9), (8, 1), (4, 4), (6, 3), (0, 0), (7, 9), (3, 8), (2, 0), (1, 8), (8, 8), (4, 3), (9, 5), (5, 2)] 

After sorting:

 self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.) self.gridKeys.sort() # They're dicts, so they need to be properly ordered for further XML-analysis. print self.gridKeys 

self.gridKeys:

 [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)] 

The first element of each tuple is "x", and the second is "y". I move the objects in the list through iteration and using these keys (So, if I want to move something along the x axis, I need to go through the entire column, and this can cause a terrible problem, in which I am not able to solve).

How can I sort tuples this way ?:

 [(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), ...] 
+7
python sorting list tuples
source share
3 answers

You can use the key parameter of the sort function to sort the tuples. The key parameter function should call a value that should be used to compare two objects. So, in your case, if you want sort use only the first element in the tuple, you can do something like this

 self.gridKeys.sort(key=lambda x: x[0]) 

If you want to use only the second element in the tuple, then

 self.gridKeys.sort(key=lambda x: x[1]) 
Function

sort will pass each item in the list of the lambda function passed as a parameter to key and will use the return value to compare two objects in the list. So, in your case, let's say you have two items in a list, like this

 data = [(1, 3), (1, 2)] 

and if you want to sort the second element, you would do

 data.sort(key=lambda x: x[1]) 

First, it passes (1, 3) to the lambda function, which returns an element with index 1 , which is 3 , and will represent this tuple during the comparison. Similarly, 2 will be used for the second tuple.

+13
source share

That should do the trick

 import operator self.gridKeys.sort(key=operator.itemgetter(1)) 
+3
source share

While fourtheye's solution is correct in the strict sense, this is exactly what you requested in the title. Perhaps this is not what you want. It might be better to take it a little further, sorting by the back of the tuple.

 self.gridKeys.sort(key=lambda x:tuple(reversed(x))) 

This makes you have this order:

 [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), ...] 

Instead of the first element being unordered, for example:

 [(4, 0), (9, 0), (6, 0), (1, 0), (3, 0), ...] 

What do I get when using:

 self.gridKeys.sort(key=lambda x: x[1]) 

By default, Python performs lexicographic sorting from left to right. Reversing a tuple effectively makes Python a lexicographic right-to-left view.

+2
source share

All Articles