When is List Comprehension inappropriate?
paddy3118 at googlemail.com
Thu Mar 22 06:21:17 CET 2007
On Mar 22, 4:56 am, a... at mac.com (Alex Martelli) wrote:
> Paddy <paddy3... 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)
"You just couldn't let it lie" ;-)
I just knew someone would bite, and re-write it.
Now to reel, you in. Its fish for dinner today!
More information about the Python-list