Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering List Within a List

Is there an easy way to sort a list within a list so that the values go from least to greatest or vice versa? I can so far only find information on ordering the lists themselves based off the leading value.

Here is an example:

data = [[8,7], [10,5,], [8,10]]

>> [[7,8], [5,10], [8,10]
like image 200
alienmode Avatar asked Aug 12 '26 00:08

alienmode


1 Answers

Use a list comprehension to sort each element (each list object) in data:

data = [sorted(x) for x in data]

data is now:

[[7, 8], [5, 10], [8, 10]]

You could also do this:

map(sorted, data)

Then use list on that map object to actually turn it into a list...

like image 197
blacksite Avatar answered Aug 13 '26 13:08

blacksite