efficiency of range() and xrange() in for loops

Alan Morgan amorgan at xenon.Stanford.EDU
Thu Apr 6 17:35:44 EDT 2006


In article <e12b4s$vcd$1 at news.albasani.net>,
Georg Brandl  <g.brandl-nospam at gmx.net> wrote:
>Alan Morgan wrote:
>
>>>range is giving you a real list, while xrange is giving you an xrange object.
>>>Have you tried to slice an xrange object? Or using .append on it?
>> 
>> No, I hadn't.  I presume these could all be defined.
>
>How would xrange(100).remove(1) work?

One way is by first converting the xrange to a list.  If we think of
the xrange as an efficient and space lean way to store certain types
of lists then it isn't unreasonable to return a regular list when
the conditions no longer hold.

Or extend the xrange object to allow multiple ranges and return the
union of xrange(0,1) and xrange(2,100).  No reason why you can't
define the whole range of list operations over xrange and still leave
it as a nice, lazy list (other languages do).  It's possible that no
one needs it enough to make it worthwhile (although having full fledged
lazy structures can really helpful when you need them) but it could
be done. 

Alan 
-- 
Defendit numerus



More information about the Python-list mailing list