l = range(int(1E9))

Mark Lawrence breamoreboy at yahoo.co.uk
Sat May 2 12:39:20 EDT 2015


On 02/05/2015 17:17, BartC wrote:
> On 02/05/2015 16:40, Mark Lawrence wrote:
>> On 02/05/2015 16:26, BartC wrote:
>>> On 30/04/2015 18:20, Ben Finney wrote:
>>>> Jon Ribbens <jon+usenet at unequivocal.co.uk> writes:
>>>
>>>>> If you use xrange() instead of range() then you will get an iterator
>>>>> which will return each of the numbers in turn without any need to
>>>>> create an enormous list of all of them.
>>>>
>>>> If you use Python 3 instead of the obsolescent Python 2, the ‘range’
>>>> callable has this sensible behaviour by default.
>>>
>>> When I first looked at Python 20 or so years ago this seemed to be the
>>> standard way of writing a for-loop:
>>>
>>> for i in range(N):
>>>     ....
>>>
>>> I remember being completely astonished at the time that 'range' actually
>>> created a list of values from 0 to N-1.
>
>> I first started maybe 14 years ago and the standard way of writing a for
>> loop was, and still is:-
>>
>> for item in items:
>>
>> When did this change, or has it always been this way and you were simply
>> using an idiom from other languages?
>
> Your example is the equivalent of 'forall' in other languages, where you
> iterate over the values of some collection of data.
>
> I agree that most for-loops in Pythonic code probably fall into that
> category.
>
> But for looping over a simple integer range, then using 'range' to
> denote the range (and build a list as it used to do), was how it was
> done. And earlier on people would have been porting coding code to
> Python at which point a straightforward 'for i=a to b' loop suddenly
> acquired a substantial overhead it didn't have before!
>

All you are saying is that they didn't bother reading the docs and 
learning how to write a *PYTHON* for loop.  Failing that don't bother 
using range, just directly convert your (say) C loop into Python.  I 
really don't see any issue here at all.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list