How many is "too many" with lists?
neelk at brick.cswv.com
Thu May 4 01:33:02 CEST 2000
Courageous <jkraska1 at san.rr.com> wrote:
> Neel Krishnaswami wrote:
> > [use xrange]
> x = 1000000
> while x > 0
> x = x - 1
> That seemed overbearing. So you're saying that
> for x in xrange ( 1000000 )
> is equally unexpensive?
Yes. It has pretty much the same behavior as range() (you just can't
mutate any elements), but doesn't generate a real list, instead
computing the numbers on demand:
Python 1.5.2 (#8, May 12 1999, 17:46:31) [GCC 22.214.171.124] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> n = 2**30
>>> x = range(n)
Traceback (innermost last):
File "<stdin>", line 1, in ?
>>> y = xrange(n)
More information about the Python-list