convert loop to list comprehension

bvdp at xplornet.com bvdp at xplornet.com
Fri Sep 8 19:40:17 EDT 2006


Please help my poor brain :) Every time I try to do a list
comprehension I find I just don't comprehend ...

Anyway, I have the following bit of code:

seq = [2, 3, 1, 9]
tmp = []
for a in range(len(seq)):
        tmp.extend([a]*seq[a])

which correctly returns:

[0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3]

Question is, can I do this as a list comprehension?

Thanks!




More information about the Python-list mailing list