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
Post a Comment