Sorting the elements of a list

The elements of a list could be sorted using the sort() method:

    random_list = [8, 7, 5, 2, 2, 5, 7, 5, 6, 4]    >>> random_list.sort()    >>> random_list    [2, 2, 4, 5, 5, 5, 6, 7, 7, 8]

When a list consists of a collection of strings, they are sorted in the alphabetical order:

    >>> day_of_week = ['Monday', 'Tuesday', 'Thursday',    'Friday', 'Saturday']    >>> day_of_week.sort()    >>> day_of_week    ['Friday', 'Monday', 'Saturday', 'Thursday', 'Tuesday']

Get Python Programming with Raspberry Pi now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.