There is a real difference here?

I read on a web that the second approach is better for memory if the range is larger, very big. This is real? They say that in the first approach every time the for start it is like re make the range or something like that… (then more memory)

for entry in range(0, 3):
    print(entry)
entryRange = range(0, 3)
for entry in entryRange:
    print(entry)

From Python documentation:

The advantage of the range type over a regular listor tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start , stop and step values, calculating individual items and subranges as needed).

3 Likes

In your example, the two examples are equivalent (ignoring the tiny memory footprint it takes to create a new variable and the compute it takes to add it to locals).

The for-loop doesn’t recreate the range instance, nor does get an iterable from it multiple times (just the once).

so, i just read crap lol. thanks!

To really be sure, use pass instead of the print statement and a larger range. Then use timeit. The latter is a good thing to learn.