Sorting is one of the most common operations in programming. In Python, you can easily sort lists using built-in methods and functions. Sorting helps arrange data in ascending or descending order, making it easier to read and process.
Python provides two main ways to sort lists:
-
sort()method (changes original list) -
sorted()function (returns a new list)
🔹 What is Sorting in Python?
Sorting means arranging elements in a specific order:
- Ascending → Small to Large (A → Z, 1 → 100)
- Descending → Large to Small (Z → A, 100 → 1)
🔹 1. Using sort() Method
The sort() method modifies the original list.
Syntax:
list.sort()
🔹 Example 1: Sort Numbers (Ascending)
numbers = [5, 2, 9, 1, 7]
numbers.sort()
print(numbers)
Output:
[1, 2, 5, 7, 9]
🔹 Example 2: Sort Strings (Alphabetical Order)
fruits = ["banana", "apple", "mango", "cherry"]
fruits.sort()
print(fruits)
Output:
['apple', 'banana', 'cherry', 'mango']
🔹 2. Sort in Descending Order
You can sort in reverse order using:
reverse=True
🔹 Example 3: Descending Order
numbers = [5, 2, 9, 1, 7]
numbers.sort(reverse=True)
print(numbers)
Output:
[9, 7, 5, 2, 1]
🔹 3. Using sorted() Function
The sorted() function returns a new sorted list and does NOT change the original list.
Syntax:
sorted(iterable)
🔹 Example 4: Using sorted()
numbers = [4, 1, 8, 3, 7]
new_list = sorted(numbers)
print("Original:", numbers)
print("Sorted:", new_list)
Output:
Original: [4, 1, 8, 3, 7]
Sorted: [1, 3, 4, 7, 8]
🔹 4. sorted() in Descending Order
numbers = [10, 3, 6, 2, 8]
new_list = sorted(numbers, reverse=True)
print(new_list)
Output:
[10, 8, 6, 3, 2]
🔹 5. Difference Between sort() and sorted()
| Feature | sort() | sorted() |
|---|---|---|
| Changes original list | ✅ Yes | ❌ No |
| Returns new list | ❌ No | ✅ Yes |
| Usage | list only | any iterable |
| Memory | Efficient | Uses extra memory |
🔹 6. Sort with Custom Key
You can sort using a custom rule with key=.
🔹 Example 5: Sort by Length of Words
words = ["banana", "apple", "kiwi", "mango"]
words.sort(key=len)
print(words)
Output:
['kiwi', 'apple', 'mango', 'banana']
🔹 7. Sort by Second Element (Tuples)
data = [(1, 5), (3, 2), (4, 8), (2, 1)]
data.sort(key=lambda x: x[1])
print(data)
Output:
[(2, 1), (3, 2), (1, 5), (4, 8)]
🔹 8. Sorting Mixed Data (Real Example)
students = ["John", "Alex", "Bob", "Christopher"]
students.sort()
print(students)
Output:
['Alex', 'Bob', 'Christopher', 'John']
🔹 9. Real-Life Use Case
Sorting product prices:
prices = [120, 50, 300, 80, 200]
prices.sort()
print("Lowest to Highest:", prices)
Output:
Lowest to Highest: [50, 80, 120, 200, 300]
🔹 Key Points to Remember
-
sort()→ changes original list -
sorted()→ creates new list -
Use
reverse=Truefor descending order -
Use
key=for custom sorting rules - Works with numbers, strings, and tuples
🚀 Conclusion
Python sorting is simple but very powerful. Whether you're working with numbers, text, or complex data structures, list sorting helps organize data efficiently.
Mastering sort() and sorted() is essential for real-world Python development, especially in data analysis, web apps, and automation.


0 Comments