• + 1 comment

    Hi Gregorz

    your solution works good. Nevertheless, you can improve it.

    TL;DR

    you can utilize list instead of queue. Your code will be 4x times faster. For big data it make sence.

    In details

    Keep in mind, that special data types like those provided by queue, give you extra features over built-in Python data types like list, set, tuple, dict, str. But this is not for free. More lines of code (inside queue objects) means more internal calls, means more time cost for trivial operations.

    Thus, if you need only push and pop and only on the end of a list, then Python built-in list is the best solution.

    Then, when to use queue? RTFM:

    The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads.

    Happy pythoning !

    😎