Python | Sort list containing alphanumeric values

19 Jul, 2019
Given a list containing both alphanumeric values, write a Python program to sort the given list in such a way that the alphabetical values always comes after numeric values.
Examples:
Input : ['k', 5, 'e', 3, 'g', 7, 0, 't']
Output : [0, 3, 5, 7, 'e', 'g', 'k', 't']

Input : [1, 'c', 3, 2, 'a', 'b']
Output : [1, 2, 3, 'a', 'b', 'c']

Approach 1 : Using sort() method
To use Python sort() method we need to convert all list values to str type first. Now there are two methods to convert values to string.
Method #1 : List comprehensionPython list comprehension can be simply used to convert each element of list to string type. We sort it and since all values are now str type, we change the final list to back to its original form.# Python3 program to Sort list # containing alpha and numeric values  def sort(lst):    lst = [str(i) for i in lst]    lst.sort()    lst = [int(i) if i.isdigit() else i for i in lst ]    return lst              # Driver codelst = ['k', 5, 'e', 3, 'g', 7, 0, 't']print(sort(lst))Output:[0, 3, 5, 7, 'e', 'g', 'k', 't']

# Python3 program to Sort list # containing alpha and numeric values  def sort(lst):    lst = [str(i) for i in lst]    lst.sort()    lst = [int(i) if i.isdigit() else i for i in lst ]    return lst              # Driver codelst = ['k', 5, 'e', 3, 'g', 7, 0, 't']print(sort(lst))
[0, 3, 5, 7, 'e', 'g', 'k', 't']

Method #2 : Using key functionKey function serves as a key for the sort comparison, which is equal to str in our case.# Python3 program to Sort list # containing alpha and numeric valuesdef sort(lst):      lst.sort(key = str)    return lst  # Driver codelst = ['k', 5, 'e', 3, 'g', 7, 0, 't']print(sort(lst))Output:[0, 3, 5, 7, 'e', 'g', 'k', 't']

Key function serves as a key for the sort comparison, which is equal to str in our case.
# Python3 program to Sort list # containing alpha and numeric valuesdef sort(lst):      lst.sort(key = str)    return lst  # Driver codelst = ['k', 5, 'e', 3, 'g', 7, 0, 't']print(sort(lst))
[0, 3, 5, 7, 'e', 'g', 'k', 't']

Approach 2 : sorted()
Alternatively, you can also use Python’s in-built function sorted() for the same purpose. Simplest difference between sort() and sorted() is: sort() doesn’t return any value while, sorted() returns an iterable list. Now there are again two ways of using sorted().
Method #1 : Using key function# Python3 program to Sort list # containing alpha and numeric values  def sort(lst):      return sorted(lst, key = str)              # Driver codelst = ['k', 5, 'e', 3, 'g', 7, 0, 't']print(sort(lst))Output:[0, 3, 5, 7, 'e', 'g', 'k', 't']

# Python3 program to Sort list # containing alpha and numeric values  def sort(lst):      return sorted(lst, key = str)              # Driver codelst = ['k', 5, 'e', 3, 'g', 7, 0, 't']print(sort(lst))
[0, 3, 5, 7, 'e', 'g', 'k', 't']

Method #2 : lambda# Python3 program to Sort list # containing alpha and numeric values  def sort(lst):          return sorted(lst, key = lambda x: (isinstance(x, str), x))              # Driver codelst = ['k', 5, 'e', 3, 'g', 7, 0, 't']print(sort(lst))Output:[0, 3, 5, 7, 'e', 'g', 'k', 't']

# Python3 program to Sort list # containing alpha and numeric values  def sort(lst):          return sorted(lst, key = lambda x: (isinstance(x, str), x))              # Driver codelst = ['k', 5, 'e', 3, 'g', 7, 0, 't']print(sort(lst))
[0, 3, 5, 7, 'e', 'g', 'k', 't']

ManasChhabra2
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers