In this post:
- Python sort list of integers (original)
- Python reverse list
- Python sort ascending list of strings into new list
- Python sort descending into new list
- Python sort list of lists, objects
- Python sort list of lists by second or third element
- Python sort descending list of lists by second element
Python's list can be sorted by:
- listSorted = sorted(numberList) - return new list; it's working on other iterables like maps
- numberList.sort() - modifying the original list and return None
- sorted(numberList, reverse=True) - descending order
- sorted(mergeList, key=itemgetter(1)) - sort list of lists by second element of the sub list.
- list(reversed(numberList)) - this will reverse the list
Here you can find several examples:
Python sort list of integers (original)
Using method sort() of list is appropriate for situation when you want to sort the orignal list.
numberList = [3, 5, 2, 4, 1]
numberList.sort()
print(numberList)
result:
[1, 2, 3, 4, 5]
Python reverse list
Reversing the list order is done by function reversed which doesn't return list and the result should be converted to list:
numberList = [3, 5, 2, 4, 1]
reverseList = list(reversed(numberList))
print(reverseList)
result:
[1, 4, 2, 5, 3]
Python sort ascending list of strings into new list
If you want to preserve the original list in the same order you can produce new list by function sorter(). In case of no parameters default sorting is ascending:
numberList = [3, 5, 2, 4, 1]
wordList = ['Java', 'C++', 'Python', 'HTML', 'CSS']
numberListAscSorted = sorted(numberList)
wordListAscSorted = sorted(wordList)
print(numberListAscSorted)
print(wordListAscSorted)
result:
[1, 2, 3, 4, 5]
['C++', 'CSS', 'HTML', 'Java', 'Python']
Python sort descending into new list
You can provide parameter : reverse=True which will change the sort order to descending:
numberList = [3, 5, 2, 4, 1]
wordList = ['Java', 'C++', 'Python', 'HTML', 'CSS'
numberListDescSorted = sorted(numberList, reverse=True)
wordListDescSorted = sorted(wordList, reverse=True)
print(numberListDescSorted)
print(wordListDescSorted)
result:
[5, 4, 3, 2, 1]
['Python', 'Java', 'HTML', 'CSS', 'C++']
Python sort list of lists, objects
If you want sort complex lists( list of lists ) when you can do it with the same sorted() function:
numberList = [3, 5, 2, 4, 1]
wordList = ['Java', 'C++', 'Python', 'HTML', 'CSS']
mergeList = []
for i in range(len(numberList)):
mergeList.append([numberList[i], wordList[i]])
print(mergeList)
mergeListSorted = sorted(mergeList)
print(mergeListSorted)
result:
[[3, 'Java'], [5, 'C++'], [2, 'Python'], [4, 'HTML'], [1, 'CSS']]
[[1, 'CSS'], [2, 'Python'], [3, 'Java'], [4, 'HTML'], [5, 'C++']]
Python sort list of lists by second or third element
In case that you wanto to chose different element than the first one for the sort you can use: key=itemgetter(1) and give the number of desired element: 1 - is the second, 2 is the third and so on..
from operator import itemgetter
numberList = [3, 5, 2, 4, 1]
wordList = ['Java', 'C++', 'Python', 'HTML', 'CSS']
mergeList=[]
for i in range(len(numberList)):
mergeList.append([numberList[i], wordList[i], wordListDescSorted[i]])
mergeSortedSecond = sorted(mergeList, key=itemgetter(1))
mergeSortedThird = sorted(mergeList, key=itemgetter(2))
print(mergeSortedSecond)
print(mergeSortedThird)
result:
[[5, 'C++', 'Java'], [1, 'CSS', 'C++'], [4, 'HTML', 'CSS'], [3, 'Java', 'Python'], [2, 'Python', 'HTML']]
[[1, 'CSS', 'C++'], [4, 'HTML', 'CSS'], [2, 'Python', 'HTML'], [5, 'C++', 'Java'], [3, 'Java', 'Python']]
Python sort descending list of lists by second element
You can sort list of objects by N-th element using: itemgetter, attrgetter. If you like to change also the order to descending then you have to provide third parameter:
sorted(mergeList, key=itemgetter(1), reverse=True)
and for objects(this is attribute of the object):
sorted(mergeList, key=attrgetter('name'), reverse=True)
from operator import itemgetter, attrgetter
numberList = [3, 5, 2, 4, 1]
wordList = ['Java', 'C++', 'Python', 'HTML', 'CSS']
mergeList=[]
for i in range(len(numberList)):
mergeList.append([numberList[i], wordList[i], wordListDescSorted[i]])
mergeSortedDescSecond = sorted(mergeList, key=itemgetter(1), reverse=True)
print(mergeSortedDescSecond)
result:
[[2, 'Python', 'HTML'], [3, 'Java', 'Python'], [4, 'HTML', 'CSS'], [1, 'CSS', 'C++'], [5, 'C++', 'Java']]
Whole code example
The whole code example is below:
#
# list reverse
#
numberList = [3, 5, 2, 4, 1]
reverseList = list(reversed(numberList))
print(reverseList)
#
# sort original list
#
numberList.sort()
print(numberList)
#
# sort ascending
#
numberList = [3, 5, 2, 4, 1]
wordList = ['Java', 'C++', 'Python', 'HTML', 'CSS']
numberListAscSorted = sorted(numberList)
wordListAscSorted = sorted(wordList)
print(numberListAscSorted)
print(wordListAscSorted)
#
# sort descending
#
numberListDescSorted = sorted(numberList, reverse=True)
wordListDescSorted = sorted(wordList, reverse=True)
print(numberListDescSorted)
print(wordListDescSorted)
#
# Sort list of lists
#
mergeList = []
for i in range(len(numberList)):
mergeList.append([numberList[i], wordList[i]])
mergeListSorted = sorted(mergeList)
print(mergeListSorted)
#
# Sort on second or third element of sub list
#
from operator import itemgetter, attrgetter
mergeList=[]
for i in range(len(numberList)):
mergeList.append([numberList[i], wordList[i], wordListDescSorted[i]])
mergeSortedSecond = sorted(mergeList, key=itemgetter(1))
mergeSortedThird = sorted(mergeList, key=itemgetter(2))
print(mergeSortedSecond)
print(mergeSortedThird)
#
# Sort on second descending
#
mergeSortedDescSecond = sorted(mergeList, key=itemgetter(1), reverse=True)
print(mergeSortedDescSecond)