List Sorting

  • This lesson teaches how to sort list elements in ascending or descending order in Python.


  • What is List Sorting? 

    List sorting means arranging the elements of a list in a specific order.

    👉 The order can be:

    • Ascending (small → big, A → Z)

    • Descending (big → small, Z → A)

    Sorting helps in:

    • Organizing data

    • Searching easily

    • Making reports

    • Ranking items

    In Python, sorting is mainly done using:

    Method

    Description

    sort()

    Sorts the original list

    sorted()

    Returns a new sorted list

    Daily Life Example

    Think about:

    • Arranging books by name

    • Ranking students by marks

    • Sorting products by price

    All these are examples of sorting data.

    Method 1: sort() (Changes Original List)

    • It is a list method

    • Sorts the list permanently

    • Default sorting is ascending

marks = [85, 72, 90, 60, 78]
marks.sort()
print(marks)

Descending Order

marks.sort(reverse=True)
print(marks)
  • Method 2: sorted() (Creates New List)

    • It is a function

    • Original list remains unchanged

    • Returns a new sorted list

numbers = [5, 2, 9, 1]
new_list = sorted(numbers)
print(new_list)
print(numbers)

Sorting Strings

Alphabetical sorting (A–Z)

names = ["Rahul", "Amit", "Zoya", "Neha"]
names.sort()
print(names)

Sorting Using Key

Used when sorting based on length or custom rule.

words = ["apple", "kiwi", "banana"]
words.sort(key=len)
print(words)

Student Ranking

students_marks = [55, 89, 72, 91, 60]
students_marks.sort(reverse=True)
print("Top Scores:", students_marks)