Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Numeric Sort in Mixed Pair String List
Sometimes we need to sort lists containing mixed data types, particularly strings that include numeric values. Python provides several approaches to perform numeric sorting on mixed pair string lists where each element contains both text and numbers.
Understanding the Problem
We have a list where each element is a string containing a name and a number (like "Daniel 4", "Oliver 7"). The goal is to sort this list based on the numeric values rather than alphabetically. For example ?
Using Custom Key Function
Define a function that extracts the numeric part for sorting ?
# Initialize the mixed pair string list
mixed_pairs = ['Daniel 4', 'Oliver 7', 'Jack 3', 'Henry 9', 'James 8', 'Benjamin 6', 'Jackson 5', 'Ryan 2']
# Define the function to extract numeric value
def get_numeric_value(pair):
name, number = pair.split()
return int(number)
# Sort using the custom key function
sorted_list = sorted(mixed_pairs, key=get_numeric_value)
# Print the sorted list
print("Sorted by numeric value:")
for item in sorted_list:
print(item)
Sorted by numeric value: Ryan 2 Jack 3 Daniel 4 Jackson 5 Benjamin 6 Oliver 7 James 8 Henry 9
Using Lambda Function
A more concise approach using lambda for inline sorting ?
# Initialize the mixed pair string list
mixed_pairs = ['Daniel 4', 'Oliver 7', 'Jack 3', 'Henry 9', 'James 8', 'Benjamin 6', 'Jackson 5', 'Ryan 2']
# Sort using lambda function (ascending order)
ascending_sort = sorted(mixed_pairs, key=lambda item: int(item.split()[1]))
print("Ascending order:", ascending_sort)
# Sort in descending order
descending_sort = sorted(mixed_pairs, key=lambda item: int(item.split()[1]), reverse=True)
print("Descending order:", descending_sort)
Ascending order: ['Ryan 2', 'Jack 3', 'Daniel 4', 'Jackson 5', 'Benjamin 6', 'Oliver 7', 'James 8', 'Henry 9'] Descending order: ['Henry 9', 'James 8', 'Oliver 7', 'Benjamin 6', 'Jackson 5', 'Daniel 4', 'Jack 3', 'Ryan 2']
Using sort() Method
Modify the original list in-place using the sort() method ?
# Initialize the mixed pair string list
mixed_pairs = ['Daniel 4', 'Oliver 7', 'Jack 3', 'Henry 9', 'James 8', 'Benjamin 6', 'Jackson 5', 'Ryan 2']
# Sort in-place using sort() method
mixed_pairs.sort(key=lambda x: int(x.split()[1]))
print("List sorted in-place:")
for item in mixed_pairs:
print(item)
List sorted in-place: Ryan 2 Jack 3 Daniel 4 Jackson 5 Benjamin 6 Oliver 7 James 8 Henry 9
Comparison of Methods
| Method | Creates New List? | Time Complexity | Best For |
|---|---|---|---|
Custom Function + sorted()
|
Yes | O(n log n) | Complex extraction logic |
Lambda + sorted()
|
Yes | O(n log n) | Simple, concise sorting |
Lambda + sort()
|
No (in-place) | O(n log n) | Memory-efficient sorting |
Conclusion
Use lambda functions with sorted() for simple numeric sorting of mixed pair strings. For complex extraction logic, define a custom key function. The sort() method provides in-place sorting when memory efficiency is important.
