Sometimes, while working with Python, we can have a problem in which we need to sort a particular list which has mixed data types. That it contains integers and strings and we need to sort each of them accordingly. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using
sort() + comparator
This problem can be solved using sort functionality provided by Python. We can construct our own custom comparator to complete the task of mixed sort.
The original list : [4, 'gfg', 2, 'best', 'is', 3] List after mixed sorting : [2, 3, 4, 'best', 'gfg', 'is']
Method #2 : Using
sorted() + key + lambda + isdigit()
The combination of above functionalities can also be used to achieve solution to this problem. In this, we just sort the list using sorted() using key functionality using lambda function to segregate digits using isdigit().
The original list : ['4', 'gfg', '2', 'best', 'is', '3'] List after mixed sorting : ['2', '3', '4', 'best', 'gfg', 'is']
- Python | Convert mixed data types tuple list to string list
- Python | Lexicographically smallest string in mixed list
- Python | Find Mixed Combinations of string and list
- Python | Count String occurrences in mixed list
- Python - Maximum column values in mixed length 2D List
- Python | Convert numeric String to integers in mixed List
- Python | Multiply Integer in Mixed List of string and numbers
- Python | Sort Flatten list of list
- Python | Sort the values of first list using second list
- Python | Sort list according to other list order
- Python | Sort list of list by specified index
- Python list sort()
- Python | How to sort a list of strings
- Python | Sort a list of percentage
- Python | Sort numeric strings in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.