Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python how to sort this list?

Tags:

python

list

lst = [(1,(1,3,5)), (5,(2,3,4)),(3,(2,3,4))]

I want to sort by the first value, descending order.

like image 734
user469652 Avatar asked Jan 10 '11 05:01

user469652


People also ask

Can you sort a 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.

What is sort () in Python?

Python sorted() Function The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically. Note: You cannot sort a list that contains BOTH string values AND numeric values.

How do you sort a list inside 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.


2 Answers

just like this:

sorted(lst, reverse=True)
like image 69
mouad Avatar answered Sep 18 '22 20:09

mouad


Sort in place? Use:

lst.sort(reverse=True)
like image 27
Mark Tolonen Avatar answered Sep 20 '22 20:09

Mark Tolonen