News for hackers. Stuff that matters.
Re:Time complexity of operations on Python's built-in types by krautsauer on Saturday August 29, 2026 @01:09PM (#49491511)
One reason you don't see a data structure with O(log(n)) operations in this list is that priority queues/heaps are not a built-in type. Weirdly, there isn't a type for them at all, just a bunch of functions (good luck if you use them wrong). https://docs.python.org/3/library/heapq.html
Re:Time complexity of operations on Python's built-in types by Retr0id on Saturday August 29, 2026 @04:09PM (#49492926)
It's not exposed for general use (yet?) but cpython does internally contain an implementation of the HAMT data structure: https://github.com/python/cpython/blob/main/Python/hamt.c

Set/Delete/Lookup are all O(log(n))

See also: https://github.com/MagicStack/immutables (for something you can actually use)

Re:Time complexity of operations on Python's built-in types by zahlman on Saturday August 29, 2026 @03:12PM (#49492457)
There isn't a type for them for the same kind of reasons that `join` is a method on the joining string. That is, it lets you reuse that code for multiple sequence types, including ones that don't exist yet. This is just something that happens with ad-hoc polymorphism, but it's also good to keep class interfaces small and implement other functionality in terms of them. Herb Sutter would approve.

Making the functions into methods wouldn't make them easier to use, it would just make the abstraction feel more familiar to those from a Java tradition rather than a C++ one.

Re:Time complexity of operations on Python's built-in types by krautsauer on Sunday August 30, 2026 @07:04AM (#49497610)
Easier I don't know, but safer for sure.

With the current implementation you can accidentally use first heapify_max and then heappop (forgetting the _max), accidentally append something through the normal list append method, change the priority of something unknowing that that breaks the invariant, or run into problems with "Tuple comparison breaks for (priority, task) pairs if the priorities are equal and the tasks do not have a default comparison order".

These headaches could have been mostly removed if these were in a class. And the option to use a custom sequence type could have surely been preserved.