Sort Numeric Strings in a List - Python Last Updated : 11 Jul, 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 info M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 5 min read Recursion in Python 6 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 5 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like