>>1513928You meant to write a = [] for the second example
Not to get too in the weeds, but the second example works in a fairly straightforward way, for each item in y if the element is greater than 2 it will append it to a.
The first method creates a generator object (which will spit out, in order, each of the items that will go into a) and then converts that generator object into list. The generator object is specifically designed to be converted into a list (or other iterable) and therefore shortcuts for the process are probably written into the code for Python.
For your specific example, the lists are too short to see many advantage with list comprehensions (it might even be slower), but for longer lists the shortcuts will start to add up.
>>1514029I've never understood why people say that list comprehensions are harder to read. Maybe it's because I come from a math background (and that's similar to how we write sets), but all the breaks and stuff in the second example make it far less natural to read IMO. But even if I didn't have a math background, Python list comprehensions can simply be read aloud to get a perfectly sensible English description of the list elements.
Also, I believe list comprehensions are more optimized in the backend. I'm not sure exactly how, but I'd guess that they allocate the necessary array spaces at once rather than each time you run out like you'd have to do for the second method