Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109202

Re: why for loop print only once after add if statement

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 <mailman.7.1464436471.1839.python-list@python.org> (permalink)
References <4743c1df-de66-44e1-9d27-b62582d757ac@googlegroups.com> <nic0t5$666$1@ger.gmane.org>
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 <python-python-list@m.gmane.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nic0t5$666$1@ger.gmane.org>
X-Mailman-Original-References <4743c1df-de66-44e1-9d27-b62582d757ac@googlegroups.com>
Xref csiph.com comp.lang.python:109202

Show key headers only | View raw


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?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

why for loop print only once after add if statement jobmattcon@gmail.com - 2016-05-28 03:19 -0700
  Re: why for loop print only once after add if statement Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-28 03:49 -0700
  Re: why for loop print only once after add if statement Peter Otten <__peter__@web.de> - 2016-05-28 13:54 +0200
  Re: why for loop print only once after add if statement Steven D'Aprano <steve@pearwood.info> - 2016-05-28 22:02 +1000
  Re: why for loop print only once after add if statement meInvent bbird <jobmattcon@gmail.com> - 2016-05-28 05:31 -0700
  Re: why for loop print only once after add if statement meInvent bbird <jobmattcon@gmail.com> - 2016-05-28 05:35 -0700
  Re: why for loop print only once after add if statement Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-05-28 12:59 -0400

csiph-web