
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Numeric Sort in Mixed Pair String List in Python
The given problem statement is to perform numeric sorting operations in a mixed pair string list with the help of Python. Sometimes we are required to sort the mixed data type list in Python so this article will be helpful to get the desired sorted form for the given dataset.
Understanding the Problem
The problem at hand is to perform the numeric sorting in the mixed pair in the given string list. So we will be given a list in which there are the mixed data types like string and integers. So we have to sort them on the basis of the numeric values in the particular element. For example let's see the below image to understand more clearly

To solve the above problem we can have multiple options as every problem can be solved using different approaches. In this article we will discuss most of the approaches.
First Approach - Algorithm
Step 1 In this approach we will first initiate the mixed pair string numeric list as mixed_list.
Step 2 Then define the function as sorting_mixed_pair and in this function we will pass the above initiated mixed_list as input.
Step 3 Inside the above function we will split the items of the list and convert the second item as integer.
Step 4 Using the sorted method we will sort the given list by using the above function as a key function to show the sorting order of the pairs of the given list.
Step 5 After sorting the mixed pairs we will print it on the console using print function.
Example
# Initialize the mixed pair string list mixed_pair = ['Daniel 4', 'Oliver 7', 'Jack 3', 'Henry 9', 'James 8', 'Benjamin 6', 'Jackson 5', 'Ryan 2'] # Define the function to get the mixed sorted list def sorting_mixed_pair(mixed_pair): name, id = mixed_pair.split() return int(id) # Call the above created function sorted_list = sorted(mixed_pair, key=sorting_mixed_pair) # Print the sorted mixed pair string list print("The numeric sorting mixed pair string list \n ", sorted_list)
Output
The numeric sorting mixed pair string list ['Ryan 2', 'Jack 3', 'Daniel 4', 'Jackson 5', 'Benjamin 6', 'Oliver 7', 'James 8', 'Henry 9']
Second Approach - Algorithm
Step 1 Initiate the mixed pair string numeric list as mixed_list.
Step 2 Define the function called sorting_mixed_pair and this function will mixed_list as input.
Step 3 Inside the above function the lambda function will be used to split the given pairs and convert the second value as integer. And also we will sort the list using a sorted method but in reverse order.
Step 4 Call the above created function and print the sorted mixed pair string list.
Example
# Initialize the mixed pair string list mixed_pair = ['Daniel 4', 'Oliver 7', 'Jack 3', 'Henry 9', 'James 8', 'Benjamin 6', 'Jackson 5', 'Ryan 2'] # Define the function to get the mixed sorted list def sorting_mixed_pair(mixed_pair): return sorted(mixed_pair, key = lambda item: int(item.split()[1]), reverse = True) # Call the above function sorted_list = sorting_mixed_pair(mixed_pair) # Print the sorted mixed pair string list print("The numeric sorting mixed pair string list \n ", sorted_list)
Output
The numeric sorting mixed pair string list ['Henry 9', 'James 8', 'Oliver 7', 'Benjamin 6', 'Jackson 5', 'Daniel 4', 'Jack 3', 'Ryan 2']
Complexity
The time complexity for the above approaches to numeric sort the mixed pair string list is O(n log n), here n is the number of items present in the given mixed pair list. As we have used a sorted method in both the approaches so the time complexity for the sorted method is O(n log n).
Conclusion
As we have used two approaches to solve the given problem of numeric sort in mixed pair string list in python. The above mentioned methods are very likely to each other and the difference is that we are using the lambda function in the second approach. That is why both the methods have similar time complexity.