Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: why for loop print only once after add if statement Date: Sat, 28 May 2016 13:54:10 +0200 Organization: None Lines: 46 Message-ID: References: <4743c1df-de66-44e1-9d27-b62582d757ac@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 4jAphJUbOEUeTOHcr5hOOgAVfSM8TXE3juvVU9cQ9i7g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'executed': 0.07; 'item,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:why': 0.09; 'executed,': 0.16; 'iteration': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:after': 0.16; 'wrote:': 0.16; 'assuming': 0.22; 'item.': 0.22; 'second': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'values': 0.28; 'prints': 0.29; 'print': 0.30; 'statement': 0.32; 'third': 0.33; 'false': 0.35; 'item': 0.35; 'depends': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'means': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'your': 0.60; 'email addr:gmail.com': 0.62; 'subject:add': 0.91; 'us?': 0.91; 'writing,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8968.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <4743c1df-de66-44e1-9d27-b62582d757ac@googlegroups.com> Xref: csiph.com comp.lang.python:109202 jobmattcon@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?