chevron_left
Cookbooks
Accessing an item in a listAdding items to a listChecking if a list is emptyChecking whether a value is in a listConcatenating two listsCopying a list in PythonDifference between append and extend methodsDifference between sort and sortedFinding the index of an element in a listFinding the length of a listIterating from index oneLooping through listsModifying listsPrinting a list in reverse orderRemoving items from a listSorting a listUsing enumerate and zip at the same time
0
0
0
new
Sorting a list in Python
Programming
chevron_rightPython
chevron_rightOperations
chevron_rightList Operations
chevron_rightCookbooks
schedule Jul 1, 2022
Last updated Python
Tags tocTable of Contents
expand_more There are two main ways to sort a list in Python:
list.sort(~)
: permanently modifies the order of the original list.sorted(~)
: returns your list in a sorted order but does not actually modify the original list.
list.sort(~)
To sort the list animals
in descending alphabetical order:
Notice that nothing is returned from animals.sort(reverse = True)
. However, the original list is modified and the elements are now sorted in descending alphabetical order.
sorted(~)
To sort the list animals
in descending alphabetical order:
Notice that sorted(animals, reverse=True)
returns the sorted list. However, the original list is unmodified.
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
0
0
Enjoy our search