[issue43618] random.shuffle loses most of the elements

Tim Peters report at bugs.python.org
Wed Mar 24 15:30:00 EDT 2021


Tim Peters <tim at python.org> added the comment:

Are you sure it's "a list"? At least print out `type(questions_element)`. `random.shuffle()` doesn't contain any code _capable_ of changing a list's length. It only does indexed accessing of the list:

...
    for i in reversed(range(1, len(x))):
        # pick an element in x[:i+1] with which to exchange x[i]
        j = randbelow(i + 1)
        x[i], x[j] = x[j], x[i]

That's about all there is to it. Note that, for this purpose, it doesn't matter want `randbelow()` does, because that function never even sees `x`.

----------
nosy: +tim.peters

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43618>
_______________________________________


More information about the Python-bugs-list mailing list