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


Groups > comp.lang.python > #104998 > unrolled thread

empty clause of for loops

Started by"Sven R. Kunze" <srkunze@mail.de>
First post2016-03-16 11:23 +0100
Last post2016-03-18 12:10 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  empty clause of for loops "Sven R. Kunze" <srkunze@mail.de> - 2016-03-16 11:23 +0100
    Re: empty clause of for loops Steven D'Aprano <steve@pearwood.info> - 2016-03-16 23:08 +1100
      Re: empty clause of for loops "Sven R. Kunze" <srkunze@mail.de> - 2016-03-16 15:44 +0100
    Re: empty clause of for loops André Roberge <andre.roberge@gmail.com> - 2016-03-16 05:41 -0700
      Re: empty clause of for loops Steven D'Aprano <steve@pearwood.info> - 2016-03-16 23:59 +1100
      Re: empty clause of for loops Peter Otten <__peter__@web.de> - 2016-03-16 14:04 +0100
    Re: empty clause of for loops Palpandi <palpandi111@gmail.com> - 2016-03-18 12:10 -0700

#104998 — empty clause of for loops

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-03-16 11:23 +0100
Subjectempty clause of for loops
Message-ID<mailman.186.1458123807.12893.python-list@python.org>
Hi,

a colleague of mine (I write this mail because I am on the list) has the 
following issue:


for x in my_iterable:
     # do
empty:
     # do something else


What's the most Pythonic way of doing this?

Best,
Sven

[toc] | [next] | [standalone]


#105011

FromSteven D'Aprano <steve@pearwood.info>
Date2016-03-16 23:08 +1100
Message-ID<56e94cd9$0$22142$c3e8da3$5496439d@news.astraweb.com>
In reply to#104998
On Wed, 16 Mar 2016 09:23 pm, Sven R. Kunze wrote:

> Hi,
> 
> a colleague of mine (I write this mail because I am on the list) has the
> following issue:
> 
> 
> for x in my_iterable:
>      # do
> empty:
>      # do something else
> 
> 
> What's the most Pythonic way of doing this?


Doing what? What is the code supposed to do? What's "empty" mean as a
keyword?

If you explain what your friends wants, then perhaps we can suggest
something. Otherwise we're just guessing. I can think of at least two
different meanings:

* run the "empty" block only if my_iterable is empty at the start of the
first loop;

* run the "empty" block if my_iterable becomes empty after the first loop.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#105040

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-03-16 15:44 +0100
Message-ID<mailman.213.1458139469.12893.python-list@python.org>
In reply to#105011
On 16.03.2016 13:08, Steven D'Aprano wrote:
> Doing what? What is the code supposed to do? What's "empty" mean as a
> keyword?
>
> If you explain what your friends wants, then perhaps we can suggest
> something. Otherwise we're just guessing. I can think of at least two
> different meanings:
>
> * run the "empty" block only if my_iterable is empty at the start of the
> first loop;

Solutions of most commenters already tackled this one, which is (at 
least from my point of view) the most intuitive.

> * run the "empty" block if my_iterable becomes empty after the first loop.

I think I can imagine where this is coming from but this was not the 
initial use-case. I think Tim's answer (count approach) would provide a 
solution for this (from my point of view) rather rare use-case.


Best,
Sven

[toc] | [prev] | [next] | [standalone]


#105014

FromAndré Roberge <andre.roberge@gmail.com>
Date2016-03-16 05:41 -0700
Message-ID<f389c723-f3d9-478a-9642-eb7c45e56894@googlegroups.com>
In reply to#104998
On Wednesday, 16 March 2016 07:23:48 UTC-3, Sven R. Kunze  wrote:
> Hi,
> 
> a colleague of mine (I write this mail because I am on the list) has the 
> following issue:
> 
> 
> for x in my_iterable:
>      # do
> empty:
>      # do something else
> 
> 
> What's the most Pythonic way of doing this?
> 
> Best,
> Sven

for x in my_iterable:
   # do something

if not my_iterable:
   # do something else

André

[toc] | [prev] | [next] | [standalone]


#105018

FromSteven D'Aprano <steve@pearwood.info>
Date2016-03-16 23:59 +1100
Message-ID<56e9589b$0$1600$c3e8da3$5496439d@news.astraweb.com>
In reply to#105014
On Wed, 16 Mar 2016 11:41 pm, André Roberge wrote:

> for x in my_iterable:
>    # do something
> 
> if not my_iterable:
>    # do something else


Doesn't work for iterators. Iterators are (in general) always truthy,
whether they are empty or not.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#105019

FromPeter Otten <__peter__@web.de>
Date2016-03-16 14:04 +0100
Message-ID<mailman.200.1458133478.12893.python-list@python.org>
In reply to#105014
André Roberge wrote:

> On Wednesday, 16 March 2016 07:23:48 UTC-3, Sven R. Kunze  wrote:
>> Hi,
>> 
>> a colleague of mine (I write this mail because I am on the list) has the
>> following issue:
>> 
>> 
>> for x in my_iterable:
>>      # do
>> empty:
>>      # do something else
>> 
>> 
>> What's the most Pythonic way of doing this?
>> 
>> Best,
>> Sven
> 
> for x in my_iterable:
>    # do something
> 
> if not my_iterable:
>    # do something else
> 
> André

This will only work for sequences:

>>> items = iter("abc")
>>> bool(items)
True
>>> list(items)
['a', 'b', 'c']
>>> bool(items) # still true...
True
>>> list(items) # ... but empty
[]

[toc] | [prev] | [next] | [standalone]


#105235

FromPalpandi <palpandi111@gmail.com>
Date2016-03-18 12:10 -0700
Message-ID<e68d7749-da7d-45a6-b7fd-153540631945@googlegroups.com>
In reply to#104998
On Wednesday, March 16, 2016 at 3:53:48 PM UTC+5:30, Sven R. Kunze wrote:
> Hi,
> 
> a colleague of mine (I write this mail because I am on the list) has the 
> following issue:
> 
> 
> for x in my_iterable:
>      # do
> empty:
>      # do something else
> 
> 
> What's the most Pythonic way of doing this?
> 
> Best,
> Sven

You can do like this.

if not my_iterable:
    <do something>
for x in my_iterable:
    <do something>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web