[Tutor] increment a counter inside generator

David Knupp knupp at well.com
Thu Mar 14 16:26:08 CET 2013


On Thu, 14 Mar 2013, Steven D'Aprano wrote:
> If you have some unknown, arbitrary iterable that doesn't support len(),
> then you can use the sum() trick:
>
> it = some_unknown_iterable()
> sum(1 for x in it)

Yes, of course you are correct. This was my intention, but I chose an 
especially poorly contrived example. Thank you for the clarification.

--dk


More information about the Tutor mailing list