l = range(int(1E9))

BartC bc at freeuk.com
Sat May 2 12:17:57 EDT 2015


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!

-- 
Bartc







More information about the Python-list mailing list