When is List Comprehension inappropriate?

Alex Martelli aleax at mac.com
Thu Mar 22 05:56:12 CET 2007


Paddy <paddy3118 at googlemail.com> wrote:
   ...
> I have found that I have gone too far when I used listcomps for their
> sideeffects rather than wanting the list produced, for example the

I agree.

> second listcomp below is an expression as statement I don't want the
> list produced - just the effect on data.
> 
> >>> # some random ranges
> >>> data = [range(random.randrange(3,7)) for x in range(4)]
> >>> # but I want each range jumbled
> >>> [ random.shuffle(d) for d in data]
> [None, None, None, None]
> >>> data
> [[2, 0, 3, 1], [0, 2, 1], [3, 4, 1, 0, 2], [2, 1, 0, 3]]
> >>>
> 
> (I do know how to re-write it).

for d in data: random.shuffle(d)


Alex



More information about the Python-list mailing list