why for loop print only once after add if statement
Peter Otten
__peter__ at web.de
Sat May 28 07:54:10 EDT 2016
jobmattcon at gmail.com wrote:
> for item, i in enumerate(aa)
> print item
>
> this writing, it can print all values
>
> for item, i in enumerate(aa)
> if item == findit:
> print item
>
> this only print the first value, means it only print once then not print
> again,
Assuming
aa = ["foo", "bar", "baz"]
and
findit = "bar"
the print statement will only be executed when findit == item.
First iteration of your for loop:
item = "foo"
if "foo" == "bar": # False
print "foo" # not executed, nothing printed
Second iteration:
item = "bar"
if "bar" == "bar": # True
print "bar" # prints bar
Third iteration:
item = "baz"
if "baz" == "bar": # False
print "baz" # not executed, nothing printed
> where is wrong?
This depends on what you want to achieve. Can you tell us?
More information about the Python-list
mailing list