Often the data structures in the
collections module are better or more readable.
The built-in list type is pretty good.
collections.deque is apparently better as it is implemented as a doubly linked list.
just like the usual list type.
l can be transformed to a min-heap using
(The heap uses the convention that the elements at
2n+2 are the children of the element at
heapq.heappush(heap, ele) provides the
O(log(n)) function to put
ele in a legitimate position.
The smallest element can be accessed in
O(1) time with
l (after heapifying).
To remove the top element use
heappop(heap) which is
O(log(n)) because it has to rearrange the rest of the elements into a heap.
There is also
heappushpop(heap, ele) which is slightly faster than the two separate commands and is useful when we want to keep the heap at a constant size.
where we note that each of the functions must take the heap as their first argument.
collections.deque is the one to use.
queue.Queue() is probably not appropriate as it is intended to allow different threads to communicate.
Here it is unclear whether to use the standard
dict type or
The distinction is that
dict will throw an error on a missing key but
collections.defaultdict will provide a default value.
0 is the default value for the
int type and
 is the default value for the
Further one can specify a function to customise the default value returned.