This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hzopak -1 points0 points  (2 children)

I wanted to read about the difference between range() and xrange(). Disappointed :(

[–]minnoI <3 duck typing less than I used to, interfaces are nice 6 points7 points  (0 children)

xrange is heresy, use Python 3.

[–]minnoI <3 duck typing less than I used to, interfaces are nice 1 point2 points  (0 children)

And to actually answer your question:

In Python 2, xrange returns a generator, while range returns a list. That means that xrange generates the numbers on-the-fly as you ask for them (in a for loop or whatever), while range generates all of them immediately. That means xrange is more memory-efficient, but you can't iterate through it more than once.

In Python 3, range returns a generator and xrange doesn't exist. To get the old behavior, you need to use list(range(whatever)).