Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sort() first element of list

I have a list that contains non specific amount of elements but every first element of the nested list is an identifier, I would like to use that identifier to sort the list in order

list = [['D', 'F', 'E', 'D', 'F', 'D'],['A', 'F', 'E', 'C', 'F', 'E'],['C', 'E', 'E', 'F', 'E', 'E'],['B', 'F', 'E', 'D', 'F', 'F']]

After its sorted

list = [['A', 'F', 'E', 'C', 'F', 'E'],['B', 'F', 'E', 'D', 'F', 'F'],['C', 'E', 'E', 'F', 'E', 'E'],['D', 'F', 'E', 'D', 'F', 'D']]

I am using python 3.3.3

like image 929
DaveDave Avatar asked Jan 11 '14 21:01

DaveDave


People also ask

How do you sort a list of tuples in Python by first element?

Using sorted() function or in place sort are the ways to Sort a list of tuples by the first element in Python. Both methods need to use the key keyword. Note: key should be a function that identifies how to retrieve the comparable element from your data structure.

Can we sort a part of the list in Python?

Python lists have a built-in list. sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable.

How do you sort a list of lists by first element and then by second element in Python?

Use the sort() Function to Sort a List of Lists in Python. The sort() method sorts the list of lists in Python according to the first element of each inner list. This method makes changes in the original list itself. We use the reverse parameter to sort in descending order.

How do you extract the first element of a list?

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).


5 Answers

Python automatically sorts lists of lists by the first element. For example:

lol=[[1,2,3],[5,6,7],[0,9,9]]
sorted(lol)
[[0, 9, 9], [1, 2, 3], [5, 6, 7]]
like image 119
Dan Avatar answered Oct 01 '22 06:10

Dan


You want to use .sort() or sorted:

>>> t = [['D', 'F', 'E', 'D', 'F', 'D'], ['A', 'F', 'E', 'C', 'F', 'E'], ['C', 'E', 'E', 'F', 'E', 'E'], ['B', 'F', 'E', 'D', 'F', 'F']]
>>> t.sort(key=lambda x: x[0])  # changes the list in-place (and returns None)
>>> t
[['A', 'F', 'E', 'C', 'F', 'E'], ['B', 'F', 'E', 'D', 'F', 'F'], ['C', 'E', 'E', 'F', 'E', 'E'], ['D', 'F', 'E', 'D', 'F', 'D']]

Also note that your list needs commas between its elements. Here is the result for sorted:

>>> sorted(t)  # does not change the list but returns the sorted list
[['A', 'F', 'E', 'C', 'F', 'E'], ['B', 'F', 'E', 'D', 'F', 'F'], ['C', 'E', 'E', 'F', 'E', 'E'], ['D', 'F', 'E', 'D', 'F', 'D']]

As you can see, the latter example sorts the lists without any key argument. The former example can as well; but you mention that only the first element is a unique identifier, so there is no way to tell what the secondary criteria might be for sorting the list beyond the first element.

like image 37
Justin O Barber Avatar answered Sep 30 '22 06:09

Justin O Barber


Essentially the same as the others but uses operator.itemgetter(),

from operator import itemgetter
first_item = itemgetter(0)
new_list = sorted(original_list, key = first_item)
like image 45
wwii Avatar answered Sep 28 '22 06:09

wwii


lists.sort(key = lambda x: x[0]) Make sure you put commas between each list in the larger list.

like image 40
qwr Avatar answered Oct 01 '22 06:10

qwr


You shouldn't overwrite the builtin list constructor, list, use another name instead like this:

>>> a_list = [['D', 'F', 'E', 'D', 'F', 'D'],['A', 'F', 'E', 'C', 'F', 'E'],['C', 'E', 'E', 'F', 'E', 'E'],['B', 'F', 'E', 'D', 'F', 'F']]

To sort the list in place, use the list.sort method:

>>> a_list.sort()

>>> a_list
[['A', 'F', 'E', 'C', 'F', 'E'], ['B', 'F', 'E', 'D', 'F', 'F'], ['C', 'E', 'E', 'F', 'E', 'E'], ['D', 'F', 'E', 'D', 'F', 'D']]

The built-in function, sorted, returns a new list, which is something you didn't seem to want to do. It returns a new list, which if you no longer need the old list would waste space in memory.

Python automatically sorts on the first element. It then automatically sorts on the second, third, and so on. Using lambda as others suggested would mean you would only sort on the first element, and the following elements would be ignored.

>>> a_list = [['b', 'f'],['b', 'e'],['b', 'd'],['a', 'c'],['a', 'b'],['a', 'a'],]
>>> a_list.sort(lambda x,y : cmp(x[0], y[0]))
>>> a_list
[['a', 'c'], ['a', 'b'], ['a', 'a'], ['b', 'f'], ['b', 'e'], ['b', 'd']]

This is why the sort is described as a stable sort

>>> help(list.sort)
Help on method_descriptor:

sort(...)
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    cmp(x, y) -> -1, 0, 1
like image 21
Russia Must Remove Putin Avatar answered Oct 01 '22 06:10

Russia Must Remove Putin