For me, it's just important to be able to read and write them. (As well as dict and generator comps). The reading part is very important as some much sample code or answers via stack flow often are in the form of one of these comps. When they are 2nd nature, you have to read things many times over to get it.
I have found, since I am starting to get better with them solutions come very easy. I know the example I give above is over the top in term of how many statements are included, but really, it just looks bigger than it is really.
The premise of the question about deriving a new list inside a comp, I think is a very valid one. You can have the simplest of list comps, and all you need to do is add one item to the list dynamically.
If modify the list before the comp, looks messy, but at least you will get the same logic applied to all list items in the comp.
You could also modify the list after the list comp is finished. Less desirable I think because you are by passing the list comps logic.
For me the best way would be if you inside the list comp you could generate the new list. I am starting thing it's not possible. As I mentioned before, I can understand why a list should not be growing inside a list comp, but with the right syntax so that a new list is created after modification and it's done before its used as an iterator you could imagine it's possible.
I haven't followed all @ccc advise yet about writing it out in full and see if that gives me a better understanding, but will do that soon.
Anyway, from a beginners perspective with some previous computer language exp. I would say the earlier you can learn about list comps the better. Other things like .join gets demystified.
I mentioned the other day, I had just joined a Python group on Facebook. I was horrified at some of the advice that was being given. I put a sample below. Also in the the below, there where some other really bizarre suggestions.
I guess my point is you could do a lot worse than learning a lot about list comps in their different forms. I know that's not what is being said here. But I am finding by unlocking this door, other learning doors are opening easier. Again, in my beginners opinion, I think anyone new to Python with basic programming skills in another language should be taught these things first up.
I also want to say how lucky I feel to have been learning Python in Pythonista which brought me here were I have only had quality feedback and answers.
From Facebook, I am not trying to be smart. Just trying to point out how lucky we are here to have great people helping out.
Question
Can some one please let me know what may be the problem in this program. I am not getting any error but output is not getting displayed.Any help will be highly appreciated..
#program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number
L=[]
j=0
for i in range(1000,3001):
#Changing the integer to string to check each digit
s=str(i)
while (j<=len(s)):
if(int(s[j])%2==0):
j=j+1
L.append(s);
print L;
Answer:
L=[]
for i in range(1000,3001):
s=str(i)
j=0
while (j<len(s)):
if(int(s[j])%2==0):
flag=1
else:
flag=0
break
#print j
j=j+1
if flag==1:
L.append(s)
print L;
My answer:
def test():
return [str(i) for i in xrange(1000, 3001) if not i% 2]
print test()
i also mention generator syntax to reduce memory dependence