# I'm starting to think like a Pythonista

Erik Jones erik at myemma.com
Thu Oct 11 05:11:01 CEST 2007

```On Oct 10, 2007, at 4:16 PM, Marc 'BlackJack' Rintsch wrote:

> On Wed, 10 Oct 2007 17:03:41 -0400, brad wrote:
>
>> Bjoern Schliessmann wrote:
>>>> low_odds = [1,3,5,7,9]
>>>> # make a list containing 10 - 98 evens only
>>>> big_evens =  big_evens = [x for x in list(xrange(99)) if x % 2 ==
>>>> 0 and x >8]
>>>
>>> Why use xrange if you convert it to a full list in place? No
>>
>> What is the dis-advantage of using xrange over range in this
>> circumstance?
>
> It's an unnecessary intermediate step.

Exactly, there's no disadvantage, but the use case for xrange is for
when there is an advantage.  xrange is a lazy generator, which means
that the values in the range are generated one by one, i.e. the
entire range is never present in memory all at once.  If all your
doing is building that list then there's no advantage.  In fact, [x
for x in xrange(99)] can be though of as longhand for range(99).  Put
another way, xrange returns a generator of successive, individual
numbers over a range (optionally with a step) while range returns a
list of numbers over a range (optionally with a step).

>>> l = xrange(99)
>>> m = range(99)
>>> type(l)
<type 'xrange'>
>>> type(m)
<type 'list'>

Erik Jones

Software Developer | Emma®
erik at myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com

```