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


Groups > comp.lang.python > #105038

Re: empty clause of for loops

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Sven R. Kunze" <srkunze@mail.de>
Newsgroups comp.lang.python
Subject Re: empty clause of for loops
Date Wed, 16 Mar 2016 15:39:07 +0100
Lines 35
Message-ID <mailman.211.1458139154.12893.python-list@python.org> (permalink)
References <56E93413.6090108@mail.de> <20160316080939.136c63ee@bigbox.christie.dr>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de QiDeuD7g0lTDLQcoH7FLyQ8PmzljO2jGZ9x1vUDyvATg==
Return-Path <srkunze@mail.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'cc:addr :python-list': 0.09; 'to:addr:python.list': 0.09; 'to:addr:tim.thechases.com': 0.09; 'to:name:tim chase': 0.09; '(empty': 0.16; '1):': 0.16; 'cc:name:python list': 0.16; 'count,': 0.16; 'follow-up': 0.16; 'iteration': 0.16; 'iterator': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'thanks.': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'otherwise,': 0.20; 'work,': 0.21; 'tim': 0.24; 'header:In- Reply-To:1': 0.24; "doesn't": 0.26; 'chase': 0.29; 'code': 0.30; "i'd": 0.31; 'useful': 0.33; 'received:10.0': 0.34; 'best,': 0.35; 'false': 0.35; 'something': 0.35; 'assigned': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'things': 0.38; 'rather': 0.39; 'skip:e 20': 0.39; 'where': 0.40; 'received:de': 0.40; 'your': 0.60; 'default': 0.61; 'charset:windows-1252': 0.62; 'situation': 0.67; 'obvious': 0.76; 'processed,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1458139151; bh=ALktAE8+EjoyTkP2QRJtWXf198f57wfyP2JPcAbJX4I=; h=Subject:To:References:Cc:From:Date:In-Reply-To:From; b=GZbZQSQF/PwILve7D6JzeTnmQDsiU/wq6YhlN4lTCT2a62JzYbooFUdj5cjOUtSJn eHdXkKZgjUzuJkPFE14oknX5KkrqdoNXS0ZUMaRkM346gKnymARqtq0Ne6m5ddl1Ee qmcRzhjiES4LL+scC6ThhvSbFO3+xr1vjJdpmZBM=
In-Reply-To <20160316080939.136c63ee@bigbox.christie.dr>
X-purgate clean
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate-type clean
X-purgate-Ad Categorized by eleven eXpurgate (R) http://www.eleven.de
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate clean
X-purgate-size 1018
X-purgate-ID 154282::1458139151-00003BBD-52A969F6/0/0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
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>
Xref csiph.com comp.lang.python:105038

Show key headers only | View raw


On 16.03.2016 14:09, Tim Chase wrote:
> If you can len() on it, then the obvious way is
>
>    if my_iterable:
>      for x in my_iterable:
>        do_something(x)
>    else:
>      something_else()
>
> However, based on your follow-up that it's an exhaustible iterator
> rather than something you can len(), I'd use enumerate:
>
>    count = 0 # have to set a default since it doesn't get assigned
>              # if no iteration happens
>    for count, x in enumerate(my_iterable, 1):
>      do_something(x)
>    if not count:
>      something_else()

Interesting variation. Good to keep in mind if I encounter a situation 
where I need both (empty flag + counter). Thanks. :)

> I do a lot of ETL work, and my code often has to report how many
> things were processed, so having that count is useful to me.
> Otherwise, I'd use a flag:
>
>    empty = True
>    for x in my_iterable:
>      empty = False
>      do_something(x)
>    if empty:
>      something_else()

Best,
Sven

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


Thread

Re: empty clause of for loops "Sven R. Kunze" <srkunze@mail.de> - 2016-03-16 15:39 +0100

csiph-web