1

I have tested in Python 2.7, the two styles are the same. My confusion is, when reading first method to generate a list, I am always a bit confused if i%2 == 0 controls if we should execute the whole loop of i in range(100), or i%2 == 0 is under loop of i in range(100). I have the confusion maybe in the past I write Java and C++, thinking methods from there.

Looking for advice how to read list generation code, normally the pattern is [<something before loop> <the loop> <something after the loop>], in this case "something before loop" is 1, and "the loop" is for i in range(100) and "something after the loop" is i%2 == 0.

Also asking for advice if writing code in method 1 is good coding style in Python 2.7? Thanks.

a = [1 for i in range(100) if i%2 == 0]

print a

a=[]
for i in range(100):
    if i%2==0:
        a.append(1)

print a

Edit 1,

I also want to compare of using xrange in an explicit loop (compare to first method of list comprehension for pros and cons), for example,

a=[]
for i in xrange(100):
    if i%2==0:
        a.append(1)

print a

Edit 2,

a = [1 for i in xrange(100) if i%2 == 0]
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 2
    In Python 2, `range` actually creates the entire list in RAM, whereas `xrange` is a generator function that only calculates and returns the next value. As such, if you only want true/false values for even numbers, you could just use `a = [(i%2==0) for i in xrange(100)]` – Dan Oct 20 '16 at 07:50
  • 1
    The first method is refered to as "list comprehension". It has it's pros and cons, but mostly pros (imo). Both methods are perfectly valid in Python. – turnip Oct 20 '16 at 08:07
  • 1
    That's a strange way to create array of _1_s. By the way, both range and xrange has "full" form `range(start, stop, step)` and – agg3l Oct 20 '16 at 08:15
  • @Dan, thanks but how `xrange` is related to my question? – Lin Ma Oct 21 '16 at 05:24
  • @Petar, if my 2nd method using `xrange`, then what is the benefit of using list comprehension comparing using a loop with `xrange`? – Lin Ma Oct 21 '16 at 05:26
  • @agg3l, thanks for the reply and vote up. Any thoughts on my original question about which way is better? – Lin Ma Oct 21 '16 at 05:26
  • 1
    In the list comprehension you can and should still use the xrange :-) please see my reply below and have a nice weekend. – Marco smdm Oct 21 '16 at 07:58
  • 1
    @LinMa performance / memory usage – Dan Oct 21 '16 at 14:17
  • @Dan, could you elaborate a bit more? – Lin Ma Oct 22 '16 at 07:43
  • @Dan, vote up for your reply. I have updated my post, please refer to Edit 1 and Edit 2 part for two candidate implementations, wondering what are the pros and cons for the two solutions? – Lin Ma Oct 22 '16 at 07:46

1 Answers1

1

1) as already mentioned in python 2.7 it is usually suggested to use xrange since it will (like in C) only keep a counter that will be incremented. Instead the range is really creating in memory a whole list from 0 till 99! Maybe here you have to think, if you need the 100 included --> then please use 101 ;)

2) You got my point, the question is valid and you have to think that operation will be executed indeed "under" the loop!!

Bearing in mind that the list comprehension is quite powerful in order to create the needful!! Anyway be careful that in some cases is not so easy to read especially when you are using inside multiple variable like x,y and so on.

I would chose your first line, just take care of min and max of your array. As said maybe you have to incorporate the 100th element and you can speed up using the xrange function instead of range. a = [1 for i in range(100) if i%2 == 0]

3) A good suggestion is also to document yourself on xrange and while loop --> on stackoverflow you can find plenty of discussions looking for the speed of the two mentioned operation!! (This is only suggestion)

Hope this clarify your query! Have a nice day!

Marco smdm
  • 1,020
  • 1
  • 15
  • 25
  • Thanks Macro, what is the benefit of first method -- list comprehension in my example? – Lin Ma Oct 21 '16 at 05:25
  • BTW, Macro, vote up for your reply. I updated my post, and also want to compare of using `xrange` in an explicit loop (compare to first method of list comprehension for pros and cons)? – Lin Ma Oct 21 '16 at 05:28
  • 1
    Hi Lin Ma, If you try to open python2.7 and write down: x = range(10) you will get a list with values from 0 to 9 so 10 items(make easy a print x). If you now try it y = xrange(10) and print it, you won't get any list but an object that is indeed a counter!! This is the big difference. Now imagine with a program where you have not 10 but billions of elements, the range will create a full list, the counter it is just a c++ number that got increase by the step you choose. Isn't it super?? So with the xrange you can speed up your big data analysis, but for easy step&get a list you can use range. – Marco smdm Oct 21 '16 at 07:23
  • 1
    Last but not least what I really like is the power of xrange compare to a while loop. Please check how fast is xrange in assembly code compare to while in the link http://stackoverflow.com/questions/869229/why-is-looping-over-range-in-python-faster-than-using-a-while-loop . This means that if you can avoid the while loop and choose some xrange value this can be better for the entire program. Of course this is usually done in big processing and it is only a suggested point you can read as curiosity!! Have a nice day and hope this clarify your doubt completely!! – Marco smdm Oct 21 '16 at 07:26
  • Thanks Marco, vote up for your reply. I have updated my post, please refer to Edit 1 and Edit 2 part for two candidate implementations, wondering what are the pros and cons for the two solutions? – Lin Ma Oct 22 '16 at 07:45
  • 1
    @Lin Ma: question is easy to reply. Both are correct methods, I would prefer the first approach with the for / if in case of some more complex cases. The first case is a bit easier to read of course and can be easily implemented for small numbers as n = 100. Second case has to be preferred most of the time, especially when it is so easy to read. List comprehension is usually fast. Try to compare case with n = 10**8, in my pc I got 14.9210000038 for first way and 11.6770000458 in the second case. – Marco smdm Oct 24 '16 at 08:59
  • 1
    This can be understand from http://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops . List comprehension when easily implemented (like map() functions) can help and speed up processing. I would always suggest the second method if it is your code :) If you are making a class lesson, then you can show the first way :-) – Marco smdm Oct 24 '16 at 09:00
  • 1
    If the question is now complete, could you mark green my answer and we can close that :-) I think you get all the needful for the next implementation!! Have a nice day!! – Marco smdm Oct 24 '16 at 09:01
  • Thanks Macro, for first method, 2nd method, do you mean Edit 1 as first method and Edit 2 as 2nd method? Or you mean first method my very first line of code (above Edit 1 part)? – Lin Ma Oct 25 '16 at 01:09
  • 1
    Edit 1 = first method using for / if loops; Edit 2 = second method using list comprehension. Is it everything clear? I hope so :-) Have a nice day. – Marco smdm Oct 25 '16 at 09:09