convert loop to list comprehension

MonkeeSage MonkeeSage at
Sat Sep 9 03:00:15 CEST 2006

bvdp at wrote:
> Cool ... and damn but you guys are fast with the answers. This appears
> to work find, but in a quick and dirty test it appears that the [list]
> version takes about 2x as long to run as the original loop. Is this
> normal?

You could also do it 'functionally' with map(), but it's ugly and would
probably fail (in this case) for non-unique list indicies; it is fast

map(lambda x: tmp.extend([seq.index(x)]*x), seq)

Ps. I don't know if xrange is faster...I thought the difference was
that range created a temporary variable holding a range object and
xrange created an iterator?


More information about the Python-list mailing list