Python, C++'s STL, Data Structures and their equivalents

Motivation

Recently, I've been doing some problem-solving. Most of my peers do that in C++, but my preferred language is Python. I never felt that I had any issues in coding in Python because most of the stuff is there in Python as well, and like people say: Language doesn't matter, you should be thorough about your logic. Yesterday, during the discussion (around implementation for a particular problem) with one of my friends, he casually mentioned that he'll use a data structure (std::set/std::multiset) that will maintain sorted order in O(log n) as we perform inserts and at any time we can perform operations on it as we'd do a normal sorted array (like binary search). I hit the roadblock - because I don't have those one-liners available in Python's standard library.

Note:

  • OrderedDict maintains the order in which they were inserted in the data structure, and not on their value. Basically, it maintains sorted-ness on the timestamp of the entry.
  • Same goes with OrderedSet which some people say, it's is there in Python. But I don't know - I haven't seen it anywhere.
  • Python's default set does not maintain order. The documentation clearly says that it's an unordered collection.

What's there?

A lot of stuff has its equivalent in Python. Here's a list I've made - the equivalents I use usually.

What's left?

std::set, std::multiset, std::map and std::multimap (Feel free to comment about any others commonly used ones). We don't have these implementations in Python's standard library. We don't.
So, what to do?

sortedcontainers

While looking for options in Python and I found sortedcontainers as an awesome third-party package. Go, have a look at its documentation. It's great.
It turns out that it's not only good, but widely used as well. So much so, that it's installed in LeetCode and you can use it in its environment. A lot of times people have raised requests to install it in Codechef and Codeforces as well. Unfortunately, it isn't available on both of these platforms.
So, what does this package cover?

Where do we go from here?

The only issue remains what to do when you want to use C++ STL's features which are not available in Python's standard lib.
If you've got your environment set up, go for sortedcontainers. No fuss.
If you're coding in someone else's environment, see if you can install third-party package.
If you can, install sortedcontainers (for all sane requirements, you should be able to).
If you can't, for example, when you're on a virtual coding platform like Codeforces or when you're in an interview. If you're coding on a platform like Codeforces, I don't have much idea, to be honest. If you're in an interview, most probably you wouldn't be asked to fully code it and execute it and often they'd like to test you out on your data structures knowledge, and even in C++, they'd ask you to manually implement the said data-structure/function. But let's say you do have to use std::multiset and interviewer's okay with using STL, you can do something like this:

from bisect import bisect_left
class Multiset:
    def __init__(self):
        self.sorted_list = []

    def add_element(self, elem):
        index = bisect_left(self.sorted_list, elem)  # O(log n)
        self.sorted_list.insert(index, elem)  # O(n)

... and then you can just mention, that if you were to use sortedcontainers, you'd get the complexity O(log n) as it's implemented using Binary Trees (which it really isn't - as it's implemented using a list of lists. But you can make your case if you know how you can do it using an AVL tree).
Obviously, you should know how you'd implement it if you were to do it by yourself, just in case the interviewer asks you about it.