Open In App

Python natsorted() Function

Last Updated : 22 Dec, 2023
Like Article

In Python, developers frequently seek tools to improve code readability and functionality. The `natsorted()` function stands out as a valuable tool for sorting objects with natural ordering. While the standard `sorted()` function struggles with human-readable strings containing numbers, `natsorted()` intelligently organizes such strings more intuitively. This article explores the syntax, an example of the `natsorted()` function, providing insights for Python developers dealing with naturally ordered data.

Python natsorted() Function Syntax

Syntax : natsorted(iterable, key=None, alg=ns.PATH, signed=False, **kwargs)


  • iterable: Collection to be sorted.
  • key: (Optional) Function for comparison key.
  • alg: (Optional) Sorting algorithm (Default: ns.PATH).
  • signed: (Optional) Considers sign of numeric components (True/False).
  • kwargs: Extra parameters for sorting.

Return Type: natsorted() returns a sorted list with the same element types as the input iterable.

What is natsorted() Function in Python?

`natsorted()` is a Python function that simplifies the sorting of objects with natural ordering. It is particularly useful when dealing with strings that contain numeric components. Unlike the standard `sorted()` function, which may not handle such strings intuitively, `natsorted()` intelligently orders them in a more natural and human-readable manner. Developers often use this function to enhance code readability and functionality.

How does natsort work?

The typical lexicographic sorting algorithm treats strings as sequences of characters and compares them based on the ASCII or Unicode values of the characters. This works well for simple strings but can lead to unexpected results when sorting strings that contain numerical values.

Example: String Sorting

Let’s consider a list of fruit names and sort them using natsort:


from natsort import natsorted
fruits = ['apple10', 'apple2', 'banana5', 'banana21', 'orange']
sorted_fruits = sorted(fruits)


['apple10', 'apple2', 'banana21', 'banana5', 'orange']

Without natsort, the lexicographic sorting results in ‘apple10’ coming before ‘apple2’ and ‘banana21’ coming before ‘banana5’, which may not be the desired order. Now, let’s use natsorted:


sorted_fruits_natural = natsorted(fruits)


['apple2', 'apple10', 'banana5', 'banana21', 'orange']

With natsorted, the list is sorted based on the natural order of the embedded numbers, resulting in a more intuitive ordering of the fruit names.

This showcases how natsort can be applied to various scenarios where strings contain embedded numerical values, providing a more human-friendly sorting order.

More Python natsorted() Function Examples

There are various ways to natsorted() function in Python as a sorted way, The natsort library provides by implementing natural sorting algorithms. You can use the natsort library by installing it first:

pip install natsort

Now, let’s look at five examples of using natsort with a list of strings:

Python Sorting Numbers

In this example code sorts a list of version strings (`versions`) using natural ordering, resulting in `[‘1.2’, ‘1.9’, ‘1.10’, ‘1.21’]`, demonstrating intuitive sorting of numeric components within the versions. The `natsorted()` function intelligently handles the alphanumeric structure, producing a human-readable order.


from natsort import natsorted
versions = ["1.10", "1.9", "1.2", "1.21"]
sorted_versions = natsorted(versions)


['1.2', '1.9', '1.10', '1.21']

Time Complexity: O(n log n)
Space Complexity: O(n)

Sorting Mixed Alphanumeric Strings

In this example the code sorts a list of mixed alphanumeric strings (`mixed_data`) using the `natsorted()` function, producing a naturally ordered result, and then prints the sorted list. Output: `[‘abc9’, ‘abc22’, ‘abc45’, ‘abc123’]`.


from natsort import natsorted
mixed_data = ["abc123", "abc45", "abc9", "abc22"]
sorted_mixed = natsorted(mixed_data)

Output :

['abc9', 'abc22', 'abc45', 'abc123']

Time Complexity: O(n log n) 
Space Complexity: O(n)

Python natsorted() Function Case-insensitive Sorting

In this example the code sorts a list of strings (`case_insensitive_data`) in a case-insensitive manner using the `natsorted()` function with the `alg=natsorted.IC` parameter, and then prints the sorted result.


from natsort import natsorted
case_insensitive_data = ["Apple", "banana", "Orange", "grape"]
sorted_case_insensitive = natsorted(case_insensitive_data, alg=natsorted.IC)


['Apple', 'banana', 'grape', 'Orange']

Time Complexity: O(n log n) 
Space Complexity: O(n)

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads