How many is "too many" with lists?
Neel Krishnaswami
neelk at brick.cswv.com
Wed May 3 19:33:02 EDT 2000
Courageous <jkraska1 at san.rr.com> wrote:
> Neel Krishnaswami wrote:
> >
> > [use xrange]
>
> x = 1000000
> while x > 0
> dosomething()
> x = x - 1
>
> That seemed overbearing. So you're saying that
>
> for x in xrange ( 1000000 )
> dosomething()
>
> 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 2.7.2.1] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> n = 2**30
>>>
>>> x = range(n)
Traceback (innermost last):
File "<stdin>", line 1, in ?
MemoryError
>>>
>>> y = xrange(n)
>>> len(y)
1073741824
Neel
More information about the Python-list
mailing list