convert loop to list comprehension

MonkeeSage MonkeeSage at gmail.com
Fri Sep 8 21:00:15 EDT 2006


bvdp at xplornet.com 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
though.

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?

Regards,
Jordan




More information about the Python-list mailing list