can list comprehensions replace map?

Andrew Dalke dalke at dalkescientific.com
Fri Jul 29 02:24:47 EDT 2005


Christopher Subich wrote:
> My  naive solution:
  ...
>        for i in ilist:
>           try:
>              g = i.next()
>              count += 1
>           except StopIteration: # End of iter
>              g = None
  ...

What I didn't like about this was the extra overhead of all
the StopIteration exceptions.  Eg, 

zipfill("a", range(1000))

will raise 1000 exceptions (999 for "a" and 1 for the end of the range).

But without doing timing tests I'm not sure which approach is
fastest, and it may depend on the data set.

Since this is code best not widely used, I don't think it's something
anyone should look into either.  :)

				Andrew
				dalke at dalkescientific.com




More information about the Python-list mailing list