Not less than for myself, more often than not once I code utilizing Python, I’m coping with assortment varieties. That features the native Python Checklist, Set, and Dictionary, in addition to some third-party assortment varieties corresponding to Numpy Array and Tensorflow tensors. The latter are often a lot quicker as a result of they’re applied utilizing CPython or no matter C-related extensions.
Nevertheless, typically it’s too overwhelming to make use of them. Particularly once we are creating an utility that’s irrelevant to Knowledge Science, these libraries may be cumbersome to be concerned only for higher efficiency.
On this article, I’ll introduce a library referred to as Sorted Containers, which supplies the perfect follow when a sorted assortment sort is required. I’ll cowl the 2 main varieties from this library: Sorted Checklist and Sorted Set. I’ll give some real-world examples to make it simpler to know.
At the start, be sure you’ve put in the library utilizing pip
.
pip set up sortedcontainers