python - why the for loop and the list comprehension produce a different result? -


animals = ['lion' ,'tiger', 'lepord', 'cheetah', 'cat']  find = []  name in animals:      if name == 'lion':          find.append(name)  print (find)  find = [find.append(name) name in animals if name=='lion']  print (find) 

the loop output ['lion'] whereas list comprehension ['none'].

why it?

the none comes list method append returns none.

rather calling append in:

[find.append(name) name in animals if name=='lion'] 

write:

[name name in animals if name=='lion'] 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -