convert loop to list comprehension
Paul Rubin
http
Fri Sep 8 22:25:58 EDT 2006
"bvdp at xplornet.com" <bvdp at xplornet.com> writes:
> > FWIW, the original loop looked perfectly fine and readable and I'd
> > suggest going with that over these hacked-up listcomp solutions. Don't
> > use a listcomp just for the sake of using a listcomp.
>
> Thanks for that, Carl. I think that using the loop is probably what
> I'll end up doing. I had no idea that the listcomp thing would be quite
> a complicated as it is appearing. I had it in my mind that I was
> missing some obvious thing which would create a simple solution :)
I think even if you code a loop, it's still cleaner to use enumerate:
seq = [2, 3, 1, 9]
tmp = []
for i,a in enumerate(seq):
tmp.extend([i]*a)
More information about the Python-list
mailing list