Sort Numeric Strings in a List - Python Last Updated : 12 Feb, 2025 Comments Improve Suggest changes Like Article Like Report We are given a list of numeric strings and our task is to sort the list based on their numeric values rather than their lexicographical order. For example, if we have: a = ["10", "2", "30", "4"] then the expected output should be: ["2", "4", "10", "30"] because numerically, 2 < 4 < 10 < 30.Using sorted()We use Python's built-in sorted() function along with the key=int parameter as this converts each element to an integer for comparison during sorting. Python a = ["10", "2", "30", "4"] # Sort the list by converting each element to int during comparison res = sorted(a, key=int) print(res) Output['2', '4', '10', '30'] Explanation:key=int argument tells sorted() to convert each string to an integer before comparing them ensuring numerical order.sorted list res contains the numeric strings arranged as ["2", "4", "10", "30"].Using list.sort() We sort the list in-place using the sort() method with the same key=int parameter. Python a = ["10", "2", "30", "4"] # In-place sorting using key=int a.sort(key=int) print(a) Output['2', '4', '10', '30'] Explanation:sort() method modifies the original list, like Method 1 key=int ensures that the elements are compared as integers.original list a is updated to ["2", "4", "10", "30"].Naive Approach Using Nested LoopsWe implement a selection sort (or bubble sort) by using nested loops that compare elements by converting them to integers during each comparison. Python a = ["10", "2", "30", "4"] # Naive approach: using nested loops to sort the list in-place for i in range(len(a)): for j in range(i + 1, len(a)): if int(a[i]) > int(a[j]): a[i], a[j] = a[j], a[i] print(a) Output['2', '4', '10', '30'] Explanation:For each element, the inner loop compares it with every subsequent element and int() conversion happens during each comparison to ensure numerical order.If an element is found to be greater than a later element (numerically) then they are swapped. Comment More infoAdvertise with us Next Article Sort Numeric Strings in a List - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads How to sort a list of strings in Python In this article, we will explore various methods to sort a list of strings in Python. The simplest approach is by using sort().Using sort() MethodThe sort() method sorts a list in place and modifying the original list directly.Pythona = ["banana", "apple", "cherry"] # Sorting list in place a.sort() 2 min read Python | Sort each String in String list Sometimes, while working with Python, we can have a problem in which we need to perform the sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let's discuss certain ways in which this problem can be solved. Method #1 : Usi 4 min read Python | Alternate Sort in String list Sometimes, while working with Python list, we can have a problem in which we need to perform sorting only of alternatively in list. This kind of application can come many times. Let's discuss certain way in which this task can be performed. Method : Using join() + enumerate() + generator expression 2 min read Python | Sort Numerical Records in String Sometimes, while working with Python records we can have a problem that they may occur in name and number format in strings. These may be required to be sorted. This problem can occur in many domains in which data is involved. Let us discuss certain ways in which this task can be performed. Method # 6 min read Python | Sort list of dates given as strings To sort a list of dates given as strings in Python, we can convert the date strings to datetime objects for accurate comparison. Once converted, the list can be sorted using Python's built-in sorted() or list.sort() functions. This ensures the dates are sorted chronologically.Using pandas.to_datetim 2 min read List of strings in Python A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona = 2 min read Python - Retain Numbers in String Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for 2 min read Python program to list Sort by Number value in String Given a List of strings, the task is to write a Python program to sort list by the number present in the Strings. If no number is present, they will be taken to the front of the list. Input : test_list = ["gfg is 4", "all no 1", "geeks over 7 seas", "and 100 planets"] Output : ['all no 1', 'gfg is 4 6 min read Python - Descending Sort String Numbers Reverse Sorting a list is easy task and has been dealt with in many situations. With Machine Learning and Data Science emerging, sometimes we can get the data in the format of list of numbers but with string as data type. Generic Sort functions give erroneous result in that case, hence several other 2 min read Increment Numeric Strings by K - Python Given the Strings list we have to increment numeric strings in a list by a given integer K. ( Note that strings that are not numeric remain unchanged ) For example: In the list ["gfg", "234", "is", "98", "123", "best", "4"] if K = 6 then result will be ["gfg", "240", "is", "104", "129", "best", "10" 4 min read Like