Sorting and filtering are fundamental operations when working with data in Python. Sorting arranges elements in a specific order, while filtering extracts elements that meet a condition. Python provides built-in functions and custom approaches to perform these operations efficiently.
Why Sorting and Filtering?
Organizes data for better readability
Improves search efficiency
Helps in data analysis and processing
1. Sorting Data in Python
Python provides several ways to sort data:
- Using
sorted()
(returns a new sorted list) - Using
.sort()
(modifies the original list) - Sorting with a custom key
- Sorting dictionaries
- Sorting tuples and lists of tuples
1.1 Sorting Lists
Using sorted()
– Returns a New Sorted List
numbers = [5, 2, 9, 1, 5, 6]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # Output: [1, 2, 5, 5, 6, 9]
Using .sort()
– Sorts In-Place
numbers.sort()
print(numbers) # Output: [1, 2, 5, 5, 6, 9]
Sorting in Descending Order
numbers = [5, 2, 9, 1, 5, 6]
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers) # Output: [9, 6, 5, 5, 2, 1]
1.2 Sorting with a Custom Key
You can use the key
parameter to define a sorting criterion.
Sorting Strings by Length
words = ["apple", "banana", "kiwi", "grape"]
sorted_words = sorted(words, key=len)
print(sorted_words) # Output: ['kiwi', 'grape', 'apple', 'banana']
1.3 Sorting Lists of Tuples
Sorting by the Second Element
students = [("Alice", 22), ("Bob", 21), ("Charlie", 23)]
sorted_students = sorted(students, key=lambda x: x[1])
print(sorted_students)
# Output: [('Bob', 21), ('Alice', 22), ('Charlie', 23)]
1.4 Sorting Dictionaries
Sorting Dictionary Keys
student_scores = {"Alice": 85, "Bob": 90, "Charlie": 78}
sorted_keys = sorted(student_scores)
print(sorted_keys) # Output: ['Alice', 'Bob', 'Charlie']
Sorting by Dictionary Values
sorted_students = sorted(student_scores.items(), key=lambda x: x[1])
print(sorted_students)
# Output: [('Charlie', 78), ('Alice', 85), ('Bob', 90)]
2. Filtering Data in Python
Filtering extracts elements that satisfy a condition.
You can filter data using:
- List comprehensions
- The
filter()
function - Custom filtering conditions
2.1 Filtering with List Comprehensions
Filtering Even Numbers
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [num for num in numbers if num % 2 == 0]
print(even_numbers) # Output: [2, 4, 6]
Filtering Words Longer Than 4 Characters
words = ["apple", "hi", "banana", "cat"]
long_words = [word for word in words if len(word) > 4]
print(long_words) # Output: ['apple', 'banana']
2.2 Filtering with filter()
Function
The filter()
function applies a function to each element and keeps only the elements that return True
.
Filtering Odd Numbers
numbers = [1, 2, 3, 4, 5, 6]
odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))
print(odd_numbers) # Output: [1, 3, 5]
Filtering Words That Start with “A”
words = ["Apple", "Banana", "Avocado", "Cherry"]
a_words = list(filter(lambda word: word.startswith("A"), words))
print(a_words) # Output: ['Apple', 'Avocado']
2.3 Filtering Dictionaries
Filtering Students with Scores Above 80
student_scores = {"Alice": 85, "Bob": 90, "Charlie": 78}
top_students = {k: v for k, v in student_scores.items() if v > 80}
print(top_students) # Output: {'Alice': 85, 'Bob': 90}
3. Sorting and Filtering Combined
Sorting and Filtering a List of Dictionaries
students = [
{"name": "Alice", "age": 22, "grade": 85},
{"name": "Bob", "age": 21, "grade": 90},
{"name": "Charlie", "age": 23, "grade": 78}
]
# Filter students with grade > 80
top_students = [s for s in students if s["grade"] > 80]
# Sort by age
sorted_top_students = sorted(top_students, key=lambda s: s["age"])
print(sorted_top_students)
Output:
[{'name': 'Bob', 'age': 21, 'grade': 90}, {'name': 'Alice', 'age': 22, 'grade': 85}]
4. Performance Considerations
sorted()
is preferred over .sort()
when you need an unchanged original list
Use list comprehensions for filtering when speed matters
Use filter()
when working with large datasets
Method | Best Use Case | Performance |
---|---|---|
sorted() | Sorting new lists | ✅ Faster |
.sort() | In-place sorting | ✅ Efficient |
List comprehension | Filtering | ✅ Best for small data |
filter() | Filtering | ✅ Better for large data |
5. When to Use Sorting and Filtering?
✅ Use sorting when ordering is important (e.g., ranking, leaderboards).
✅ Use filtering when removing unwanted data (e.g., filtering low scores).
✅ Combine sorting and filtering for optimized data processing.
🚫 Avoid unnecessary sorting/filtering for very large datasets to improve efficiency.