python - Print list while excluding specific named values -
i looking basic way exclude printing item list (not remove/delete) if named value = specified, e.g. = 0
i thought basic this, error not defined.
a = 2 b = 0 c = 4 d = 0 e = 6 lis = [a,b,c,d,e] while != 0 in range(lis): print (lis[i])
wanted result:
2
4
6
seems missing simple? or not simple imagined?
python syntax doesn't support conditional you're trying apply it. there several alternatives, though:
you put condition inside loop:
for item in lis: if item != 0: print item
you filter list:
for item in filter(lambda i: i!=0, lis): # filter(none, lis) works print item
or using list comprehension (technically generator expression here):
for item in (i in lis if != 0): print item
or skip loop entirely:
print '\n'.join(str(i) in lis if != 0)
Comments
Post a Comment