Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to sort a list of strings in Python
Next article icon

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 Loops

We 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.


Next Article
How to sort a list of strings in Python
author
manjeet_04
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. [GFGTABS] Python a = ["banana", "apple", "cher
    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_dateti
    3 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. [GFGTABS
    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 fo
    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
    3 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences