Lists and Indices

Bryan Olson fakeaddress at nowhere.org
Fri Aug 9 19:13:16 CEST 2002


Jim wrote:
> On Fri, 09 Aug 2002 02:51:08 -0600, Bryan Olson wrote:
> 
> 
>>We could also define the indices function using generators, which would
>>avoid building the whole list of indices,
>>
>>     def indices(sequence):
>>         for i in range(len(sequence)):
>>             yield i
> 
> 
> Unless I misunderstand something, this still generates the whole list in
> memory and returns parts of it one-by-one - the range function does this.
> You probably meant something like:
> 
> def indices(seq):
> 	l = len(seq)
> 	i = 0
> 	while i < l:
> 		yeild i
> 		i = i + 1

Yup, that's what I should have written.  I wrote what I did to
test out the generator construct, then forgot the point of using
it.


--Bryan




More information about the Python-list mailing list