[Python-ideas] shuffled as a way to shuffle an iterable

Chris Angelico rosuav at gmail.com
Thu Sep 8 19:40:56 EDT 2016


On Fri, Sep 9, 2016 at 9:36 AM, Xavier Combelle
<xavier.combelle at gmail.com> wrote:
> and eventually this one work in all case of iterable
>
> def shuffled3(iterable):
>     result = list(iterable)
>     random.shuffle(result)
>     return result
>

This is the one obvious way to do this. Not all iterables can
logically be shuffled, so the most normal approach would be to turn it
into a list, then shuffle. Which is exactly what this does.

ChrisA


More information about the Python-ideas mailing list