Hi, from what I can see Python is one of the few major languages that lacks a collection of sorted containers in the standard library, I am just wondering if there are any obstacles that I am not seeing to get this very nice package into the standard library:
Adding something to the stdlib inevitably leads to the question of why it needs to be in the stdlib instead of just on PyPI? Is this something that’s widely needed by most people, or does it require coordination at the interface/protocol level?
In this instance I would argue the need isn’t wide enough to warrant pulling it into the stdlib.
Although, many people coming from other languages just assume a container such as dict is key-sorted, and not insertion-sorted. That leads to a bunch of subtle bugs which we could avoid if a sorted container module had more visibility in python, via being a part of stdlib for instance.
I’m not sure if that level of generalization can be made. For instance, I assume there’s no sorting on maps in other languages since that’s how they are typically designed. Plus you’re assuming people who make that sort of assumption will know to look in the stdlib instead of just then want it for dicts themselves.
If you “come from another language” the learning Python includes learning its differences. If you think something can be improved, then that’s worth discussion, but mere difference is good to note, but no reason for change.
One interesting case to consider: interviewing for jobs. In such cases it’s often not allowed to use a third-party library in a solution to a coding interview question, but using a language’s standard library is fair game. And often candidates are allowed to use whatever language they’re most comfortable with from among several options. The Python language sponsors at a polyglot company I know of were actually concerned about this exact problem quite recently, since one of the company’s standard interview questions gives candidates using e.g. Java an advantage over candidates using Python, since Java has sorted containers built in (which are needed for the correct solution) whereas Python does not.
The comment from @jab really hits the mark. For example, if you try solving this LeetCode problem https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-ii, having a sorted container type data structure in the Python standard library would be incredibly useful.
(although the sortedContianer package actually can be used on leetcode, but how about other platforms?)
Sorted containers aren’t containers that call sorted() at every mutation and/or during output. It maintains a sorted order at all times via bisection, so that all common operations can be accomplished in logarithmic time complexity. This contrasts with built-in containers, which may be able to perform certain tasks in constant time complexity but at the expense of other tasks, which have to do with a linear time complexity.
This makes sorted containers well suited to applications that require containers to be reasonably performant in all tasks rather than being excellent at only a few tasks but mediocre at others.
The python-sortedcontainers repo currently has 3.5k stars, 204 forks and 23 contributers, and is on version 2.4.0. I think those numbers show a healthy sign that such data types are objectively needed by many people and worth a consideration as part of the stdlib.
I don’t think the number of users, stars, or forks is a particularly important metric for inclusion–it can also be read to mean “this is working great as a package”. TensorFlow has 186k stars and 74k forks, but no would argue it should come standard with cPython.
But what is a good metric? This is a reoccurring question that Core devs get asked, and noone ever gives a straight answer - The only clear answer seems to be “an existing core dev wants it to be in the stdlib”, and if this is the case, essentially nothing else matters.
I had never heard of this repo before, and looking at the implementation, none of the structures in here have the performance characteristics I would want (Generally, for the work I do ordering is important, but ordering usually isn’t sorting by value) and make different tradeoffs than I would want for performance-critical code. If you want this, you can easily install it. It’s pure python, so it isn’t even like standard library inclusion is going to help ensure it’s supported cross platform better, it already is.
Leaving this out of the standard library when enough building blocks are already there also means it’s more likely people put together the right thing from that for their needs with the right performance tradeoffs.
The closest to this that I’ve needed, I’ve written myself, but with array.array and bisect, or with heapq, a tuple of (priority, value), and a list (in cases where this was not quite being used as a priority queue, for anyone wondering why I reached for this and not for that)
more useful if in the standard library than on pypi (such as things that require libraries all use it for it to work, see logging and asyncio for examples of this case)
doesn’t make tradeoffs that could be generally inappropriate.
That’s a great question. I think the real-but-unsatisfying answer is that there isn’t an objective metric–it’s a design decision and something for the language designers (the SC) to decide.
I agree a sorted container with fast insertion would be a great addition to the python standard library.
For instance using bisect to find a value in an ordered list is fast (O(log n)) but inserting a value in a sorted list is still slow (O(n)) so it makes using bisect quite useless on large data when it comes to not just reading the data.
I think @mikeshardmind gave good metrics or guidelines; a sorted container package or at least a BSTree equivalent in the STL would be a great addition, as it would provide containers with known behaviours and complexity, require minimal update (a BSTree is not something new…) or could even be written in pure python.
It is useful as it solves a known problem (at least fast insertion in a sorted list…) and is something that exists in almost all main programming languages.
Python being one of the most used programming language as of today, how come its STL provides a package to read and write WAV files, but not tools for a classic problem in computer science if the goals are to “provide standardized solutions for many problems that occur in everyday programming”?
I’m not at all criticizing the wave package nor the way packages are added in the STL. I’m simply wondering why adding a sorted container package is not being considered even though there is a real need for it.
Edit: By “STL” I meant python standard library, but I agree I should have used another acronym.
A bit of pedantry, but “STL” is a term that comes from c++ (i.e. Python doesn’t have an STL because it doesn’t have templates) and it’s usually wrong in a C++ context too (it refers to an early precursor to the C++ standard library and not the current C++ standard library).
I think the wav support in the Python standard library is a historic artifact and wouldn’t be added now (so doesn’t provide a good indication of the sort of think that should be in the Python standard library).