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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.