Doing similar operations in Python as in C++ can often be slower. list
and dict
are actually implemented very well, but you gain a lot of overhead using Python objects, which are more abstract than C++ objects and require a lot more lookup at runtime.
Incidentally, std::vector
is implemented in a pretty similar way to list
. std::map
, though, is actually implemented in a way that many operations are slower than dict
as its size gets large. For suitably large examples of each, dict
overcomes the constant factor by which it's slower than std::map
and will actually do operations like lookup, insertion, etc. faster.
If you want to use std::map
and std::vector
, nothing is stopping you. You'll have to wrap them yourself if you want to expose them to Python. Do not be shocked if this wrapping consumes all or much of the time you were hoping to save. I am not aware of any tools that make this automatic for you.
There are C API calls for controlling the creation of objects with some detail. You can say "Make a list with at least this many elements", but this doesn't improve the overall complexity of your list creation-and-filling operation. It certainly doesn't change much later as you try to change your list.
My general advice is
If you want a fixed-size array (you talk about specifying the size of a list), you may actually want something like a numpy array.
I doubt you are going to get any speedup you want out of using std::vector
over list
for a general replacement in your code. If you want to use it behind the scenes, it may give you a satisfying size and space improvement (I of course don't know without measuring, nor do you. ;) ).
dict
actually does its job really well. I definitely wouldn't try introducing a new general-purpose type for use in Python based on std::map
, which has worse algorithmic complexity in time for many important operations and—in at least some implementations—leaves some optimisations to the user that dict
already has.
If I did want something that worked a little more like std::map
, I'd probably use a database. This is generally what I do if stuff I want to store in a dict
(or for that matter, stuff I store in a list
) gets too big for me to feel comfortable storing in memory. Python has sqlite3
in the stdlib and drivers for all other major databases available.