Lists and Indices

Jim Meier jim at dsdd.org
Fri Aug 9 17:00:36 CEST 2002


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

-jim



More information about the Python-list mailing list