Cease Manually Sorting Your Listing In Python If Efficiency Is Involved | by Christopher Tao | Aug, 2024

A sorted assortment library that’s as quick as C-extensions

No less than for myself, more often than not once I code utilizing Python, I’m coping with assortment varieties. That features the native Python Listing, Set, and Dictionary, in addition to some third-party assortment varieties resembling Numpy Array and Tensorflow tensors. The latter are normally a lot quicker as a result of they’re carried out utilizing CPython or no matter C-related extensions.

Nevertheless, generally it’s too overwhelming to make use of them. Particularly after we are growing an utility that’s irrelevant to Information Science, these libraries is likely to be cumbersome to be concerned only for higher efficiency.

On this article, I’ll introduce a library known as Sorted Containers, which offers the very best follow when a sorted assortment kind is required. I’ll cowl the 2 main varieties from this library: Sorted Listing and Sorted Set. I’ll give some real-world examples to make it simpler to know.

At first, be sure to’ve put in the library utilizing pip.

pip set up sortedcontainers