I have a list as follows.
[(5,), (2,), (4,), (1,), (3,), (6,), (7,), (8,)]
How can I sort the list to get
[1,2,3,4,5,6,7,8]
or
[8,7,6,5,4,3,2,1]
?
Convert the list of tuples into a list of integers, then sort it:
thelist = [(5,), (2,), (4,), (1,), (3,), (6,), (7,), (8,)]
sortedlist = sorted([x[0] for x in thelist])
print sortedlist
See it on codepad
sorted([x[0] for x in list])? sorted is a reserved word so maybe not best to use as a variable.sorted([x[0] for x in list], reverse=True). Also, "list" is a builtin. +1 nonetheless