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

2006-04-09 Thread Fredrik Lundh
Alan Morgan wrote: > >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

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

2006-04-06 Thread Alan Morgan
In article <[EMAIL PROTECTED]>, Georg Brandl <[EMAIL PROTECTED]> 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 def

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

2006-04-06 Thread Fredrik Lundh
Carl Banks wrote: > > I wondered if the Python compiler could, as a special case, turn: > > > > for i in range(n) > > > > into compiled code equivalent to > > > > for i in itr > > > > where "itr" is a lightweight iterator that returns the same values as > > iter(range(n)). > > Nope, out of the que

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

2006-04-06 Thread Steve R. Hastings
On Wed, 05 Apr 2006 22:08:29 -0700, Carl Banks wrote: >> for i in range(n) >> >> into compiled code equivalent to >> >> for i in itr >> >> where "itr" is a lightweight iterator that returns the same values as >> iter(range(n)). > Nope, out of the question for Python 2.x. Note that the the builtin

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

2006-04-06 Thread Paddy
I wondered at the tone of some of the replies, re-read the repliess and your original message. On first readings ithought that your original message was OK and that the replies were a bit 'strong' . On second reading I thought that the original could be interpreted a little less nicely, but I had t

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

2006-04-05 Thread Georg Brandl
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? Georg -- http://mail.python.org/

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

2006-04-05 Thread Fredrik Lundh
Steve R. Hastings wrote: > > the difference isn't very > > large, xrange is actually slower in some python versions, and you'll > > need the integer objects sooner or later anyway... > > Actually, for many uses of "for i in (range|xrange)", you only need the > value of i, and you aren't doing anyt

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

2006-04-05 Thread Carl Banks
Steve R. Hastings wrote: > I wondered if the Python > compiler could, as a special case, turn: > > for i in range(n) > > into compiled code equivalent to > > for i in itr > > where "itr" is a lightweight iterator that returns the same values as > iter(range(n)). Nope, out of the question for Pytho

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

2006-04-05 Thread Azolex
Steve R. Hastings wrote: > On Thu, 06 Apr 2006 02:33:16 +0200, Azolex wrote: >> Don't. It's quite funny, thanks. > > I guess I should laugh. :-/ > > > When you read my original articles, did *you* think I was proposing that > range() be changed to always return an iterator? I thought what I wr

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

2006-04-05 Thread Felipe Almeida Lessa
Em Qua, 2006-04-05 às 19:15 -0700, Alex Martelli escreveu: > Steve R. Hastings <[EMAIL PROTECTED]> wrote: >... > > Actually, for many uses of "for i in (range|xrange)", you only need the > > value of i, and you aren't doing anything with the integer object. You > > Then, the speediest approac

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

2006-04-05 Thread Alex Martelli
Steve R. Hastings <[EMAIL PROTECTED]> wrote: ... > Actually, for many uses of "for i in (range|xrange)", you only need the > value of i, and you aren't doing anything with the integer object. You Then, the speediest approach may be completely different: import itertools for i in itertools.re

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

2006-04-05 Thread Felipe Almeida Lessa
Em Qua, 2006-04-05 às 22:24 +0200, Fredrik Lundh escreveu: > the difference isn't very > large, xrange is actually slower in some python versions, and you'll > need the integer objects sooner or later anyway... Some code is worth a thousand words: $ python2.3 Python 2.3.5 (#2, Mar 6 2006, 10:1

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

2006-04-05 Thread Azolex
Steve R. Hastings wrote: > On Thu, 06 Apr 2006 09:08:45 +1000, Steven D'Aprano wrote: >> Yes, the above example is a good use case for xrange. Did you think that >> anyone denied that there were good cases for it? > > I am now officially sorry for starting this thread. Don't. It's quite funny, th

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

2006-04-05 Thread Steve R. Hastings
On Thu, 06 Apr 2006 02:33:16 +0200, Azolex wrote: > Don't. It's quite funny, thanks. I guess I should laugh. :-/ When you read my original articles, did *you* think I was proposing that range() be changed to always return an iterator? I thought what I wrote was pretty clear... -- Steve R. Has

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

2006-04-05 Thread Alan Morgan
In article <[EMAIL PROTECTED]>, Erik Max Francis <[EMAIL PROTECTED]> wrote: >Alan Morgan wrote: > >> In article <[EMAIL PROTECTED]>, >> Giovanni Bajo <[EMAIL PROTECTED]> wrote: > > >>>Because you assume that the only use-case of range() is within a for-loop. >>>range() is a builtin function that c

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

2006-04-05 Thread Steve R. Hastings
On Thu, 06 Apr 2006 09:08:45 +1000, Steven D'Aprano wrote: > Yes, the above example is a good use case for xrange. Did you think that > anyone denied that there were good cases for it? I am now officially sorry for starting this thread. Please let me just summarize what I wanted to say: * Ques

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

2006-04-05 Thread Alan Morgan
In article <[EMAIL PROTECTED]>, Georg Brandl <[EMAIL PROTECTED]> wrote: >Alan Morgan wrote: >> In article <[EMAIL PROTECTED]>, >> Giovanni Bajo <[EMAIL PROTECTED]> wrote: >>>Steve R. Hastings wrote: >>> > in Python 2.X, range is defined to return a list. if you start > returning something

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

2006-04-05 Thread Steven D'Aprano
On Wed, 05 Apr 2006 15:02:33 -0700, Steve R. Hastings wrote: > On Wed, 05 Apr 2006 22:24:24 +0200, Fredrik Lundh wrote: >>> If Python actually allocates the list, then clearly we should all use >>> "for i in xrange". >> >> clearly we should all? speak for yourself. > > I apologize for my pithy

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

2006-04-05 Thread Georg Brandl
Alan Morgan wrote: > In article <[EMAIL PROTECTED]>, > Giovanni Bajo <[EMAIL PROTECTED]> wrote: >>Steve R. Hastings wrote: >> in Python 2.X, range is defined to return a list. if you start returning something else, you'll break stuff. >>> >>> Perhaps I'm mistaken here, but I don't see ho

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

2006-04-05 Thread Erik Max Francis
Alan Morgan wrote: > In article <[EMAIL PROTECTED]>, > Giovanni Bajo <[EMAIL PROTECTED]> wrote: > >>Because you assume that the only use-case of range() is within a for-loop. >>range() is a builtin function that can be used in any Python expression. For >>instance: >> >>RED, GREEN, BLUE, WHITE, B

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

2006-04-05 Thread Steve R. Hastings
On Wed, 05 Apr 2006 22:09:29 +, Giovanni Bajo wrote: > [...] you assume that the only use-case of range() is within a for-loop. No, I do not assume any such thing. I have not suggested that range() should be changed to always return an iterator. I wondered if the Python compiler could, as a

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

2006-04-05 Thread Alan Morgan
In article <[EMAIL PROTECTED]>, Giovanni Bajo <[EMAIL PROTECTED]> wrote: >Steve R. Hastings wrote: > >>> in Python 2.X, range is defined to return a list. if you start >>> returning something else, you'll break stuff. >> >> Perhaps I'm mistaken here, but I don't see how this optimization could >>

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

2006-04-05 Thread Giovanni Bajo
Steve R. Hastings wrote: >> in Python 2.X, range is defined to return a list. if you start >> returning something else, you'll break stuff. > > Perhaps I'm mistaken here, but I don't see how this optimization could > possibly break anything. Because you assume that the only use-case of range() i

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

2006-04-05 Thread Steve R. Hastings
On Wed, 05 Apr 2006 22:24:24 +0200, Fredrik Lundh wrote: >> If Python actually allocates the list, then clearly we should all use >> "for i in xrange". > > clearly we should all? speak for yourself. I apologize for my pithy turn of phrase. Clearly what I should have written was: "If Python act

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

2006-04-05 Thread [EMAIL PROTECTED]
Todd wrote: > Steve R. Hastings wrote: > > When you compile the expression > > > > for i in range(1000): > > pass > > > > does Python make an iterator for range(), and then generate the values > > on the fly? Or does Python actually allocate the list [0, 1, 2, ..., 999] > > and then s

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

2006-04-05 Thread Todd
Steve R. Hastings wrote: > When you compile the expression > > for i in range(1000): > pass > > does Python make an iterator for range(), and then generate the values > on the fly? Or does Python actually allocate the list [0, 1, 2, ..., 999] > and then step through it? I ran an expe

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

2006-04-05 Thread Fredrik Lundh
Steve R. Hastings wrote: > If Python actually allocates the list, then clearly we should all use > "for i in xrange". clearly we should all? speak for yourself. the difference isn't very large, xrange is actually slower in some python versions, and you'll need the integer objects sooner or late

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

2006-04-05 Thread Georg Brandl
Steve R. Hastings wrote: > When you compile the expression > > for i in range(1000): > pass > > does Python make an iterator for range(), and then generate the values > on the fly? Or does Python actually allocate the list [0, 1, 2, ..., 999] > and then step through it? It does.

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

2006-04-05 Thread John Salerno
Steve R. Hastings wrote: > If Python doesn't currently optimize this case, is there any chance this > optimization could be added? I was just asking about this, and apparently it will be changed in 3.0: Built-in Namespace * Make built-ins return an iterator where appropriate (e.g. range(),

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

2006-04-05 Thread Steve R. Hastings
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list [0, 1, 2, ..., 999] and then step through it? I was under the impression that recent releases of