shuffling elements of a list

Gerard Flanagan grflanagan at yahoo.co.uk
Thu Jun 1 09:32:44 CEST 2006


Ben Finney wrote:
[snip]
>
> Please don't write C in Python. The 'for' statement allows iteration
> directly over a sequence, no need to maintain an index. Also, the
> modulus operator is called for with your cycling of the pile index.
>
>         pile_index = 0
>         for card in deck:
>             piles[pile_index].append(card)
>             pile_index = (pile_index + 1) % numpiles
>

no need to maintain an index ;-)

        piles = [ list() for _ in range(n) ]
        for i, card in enumerate(deck):
            piles[i % numpiles].append(card)


Gerard




More information about the Python-list mailing list