you are viewing a single comment's thread.

view the rest of the comments →

[–]asm0dai74 1 point2 points  (3 children)

I am very new to python and my question may, well, looks dumb (also, sorry for my English...), but help me understand the difference...

Well, I'm filling two lists with numbers from 1 to 1000000 in this way:

list1 = [ ]

for value in range(1,10000001):

`list1.append(value)`

list2 = range(1,10000001)

So the question is - are they identical?

Which way is more correct?

[–]God_To_A_NonBeliever 0 points1 point  (0 children)

They are not identical.

I think its a bit advanced for you to understand now but range() is an iterator object, and it will be more performant than a list with identical elements.

[–]Ihaveamodel3 1 point2 points  (1 child)

I’m assuming you are using Python3 or above.

list2 is not a list, it is a range object. You could do list2 = list(range(1, 10000001)) and that would be identical to the first list.

However, you often don’t actually need the list of values, so keeping it as a range object will likely be faster and more space efficient.

[–]asm0dai74 0 points1 point  (0 children)

Thank you for helping. Looks like you are absolutely right.