You’ve mastered storing data in lists and dictionaries. Now, let’s learn how to command it. How do you find the highest value, alphabetize names, or extract only the relevant information? Python’s built-in functions sort()
, sorted()
, and filter()
are your essential tools for this.
The key to choosing the right tool often comes down to one question: Do you want to change the original list or create a new one?
1. Sorting: sort()
vs. sorted()
Both do the same thing but with a critical difference in outcome.
list.sort()
: The In-Place Organizer
This method sorts the list it is called on and returns None
. It changes your original list.
numbers = [42, 1, 17, 99]
numbers.sort() # Sorts the list in-place
print(numbers) # Output: [1, 17, 42, 99] (The original is changed!)
sorted()
: The New Copy Creator
This function takes any iterable (like a list) and returns a new, sorted list, leaving the original perfectly untouched.
numbers = [42, 1, 17, 99]
sorted_numbers = sorted(numbers) # Creates a new, sorted list
print(sorted_numbers) # Output: [1, 17, 42, 99]
print(numbers) # Output: [42, 1, 17, 99] (The original is safe!)
When to use which?
- Use
.sort()
when you are done with the original order. - Use
sorted()
when you need to preserve the original list.
2. Advanced Sorting: The Powerful key
Parameter
What if you want to sort something more complex, like a list of tuples? This is where the key
parameter shines. It lets you specify how to sort.
Example: Sort by Length
fruits = ["apple", "kiwi", "banana", "berry"]
fruits_sorted_by_length = sorted(fruits, key=len) # `len` is the function
print(fruits_sorted_by_length)
# Output: ['kiwi', 'berry', 'apple', 'banana']
Example: Sort a List of Dictionaries
A incredibly common task, especially when working with data from APIs or JSON.
users = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35}
]
# Sort the list of dictionaries by the 'age' key
users_sorted_by_age = sorted(users, key=lambda user: user['age'])
print(users_sorted_by_age)
# Output: [{'name': 'Bob', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Charlie', 'age': 35}]
The lambda
function is a simple, one-line function we define on the spot. It tells Python: "For each item in this list (which we'll call user
), use the value at user['age']
as the thing to sort by."
3. Filtering Data with filter()
The filter()
function is used to extract elements from an iterable that meet a specific condition. It's a fundamental functional programming tool.
The traditional way (with a loop):
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
evens = []
for num in numbers:
if num % 2 == 0:
evens.append(num)
print(evens) # Output: [2, 4, 6, 8]
Using filter()
:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
evens = list(filter(lambda x: x % 2 == 0, numbers)) # Filter for even numbers
print(evens) # Output: [2, 4, 6, 8]
filter()
returns a special iterator, so we wrap it in list()
to see the results.
A Note on Pythonic Style: While filter()
is a powerful and valid tool, many Python developers prefer using list comprehensions for filtering due to their exceptional readability. We will explore this preferred method in our next article.
Your Data Transformation Toolkit
With sort()
, sorted()
, and filter()
, you’ve moved from simply storing data to actively shaping it. Remember:
- Use
sorted()
to preserve original data; use.sort()
to replace it. - The
key
parameter is your key to powerful, custom sorting. -
filter()
is a fundamental tool for extracting subsets of data.
These functions form the foundation of data cleaning and preparation in Python—a crucial first step before any analysis.
Up Next: Ready for the real magic? We'll explore List Comprehensions—the concise and powerfully Pythonic technique that can often replace filter()
and map()
, making your code cleaner and more expressive.
Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.
Top comments (0)