[Tutor] Using a list comp in place of a for loop

afith13 at gmail.com afith13 at gmail.com
Sun Oct 4 00:33:47 CEST 2009


Hi tutors,

I've got a loop and a comprehension that do the same thing (as far as I can tell):

for i in range(N):
  someList.append.newObject(i)

...and...

[someList.append.newObject(i) for i in range(N)]

I'm tempted to write this as a list comp because it's a big list and I've read that list comps are faster than for loops in general. I'm hesitating because I'm not sure if building a big list of "None" objects and then throwing it away has any drawbacks.

Thanks in advance,


More information about the Tutor mailing list