The Python Game Book

code games. learn Python.

User Tools

Site Tools


Sidebar

Github:

en:glossary:s:sorting

sorting

Sorting elements, usually inside a list.

see:

Python has an in-built sorting function:

>>> mylist = [1,5,6,8,99,3,2,5,7,77,8,88,0,33]
>>> mylist.sort()
>>> mylist
[0, 1, 2, 3, 5, 5, 6, 7, 8, 8, 33, 77, 88, 99]

You can also sort reverse the elements in a list or sort in reverse order:

>>> mylist = [1,5,6,8,99,3,2,5,7,77,8,88,0,33]
>>> mylist.reverse()
>>> mylist
[33, 0, 88, 8, 77, 7, 5, 2, 3, 99, 8, 6, 5, 1]
>>> mylist.sort(reverse=True)
>>> mylist
[99, 88, 77, 33, 8, 8, 7, 6, 5, 5, 3, 2, 1, 0]
>>> 

en/glossary/s/sorting.txt · Last modified: 2020/05/31 07:14 by horst