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


Groups > comp.lang.python > #105019

Re: empty clause of for loops

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: empty clause of for loops
Date 2016-03-16 14:04 +0100
Organization None
Message-ID <mailman.200.1458133478.12893.python-list@python.org> (permalink)
References <mailman.186.1458123807.12893.python-list@python.org> <f389c723-f3d9-478a-9642-eb7c45e56894@googlegroups.com>

Show all headers | View raw


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
[]

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


Thread

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

csiph-web