Sven R. Kunze wrote:
> On 16.03.2016 16:02, Tim Chase wrote:
>> On 2016-03-16 15:29, Sven R. Kunze wrote:
>>> I would re-use the "for-else" for this. Everything I thought I
>>> could make use of the "-else" clause, I was disappointed I couldn't.
>> Hmm...this must be a mind-set thing. I use the "
Peter Otten schreef op 2016-03-16 13:57:
If you don't like exceptions implement (or find) something like
items = peek(items)
if items.has_more():
# at least one item
for item in items:
...
else:
# empty
Only if such a function is used a lot or cannot be conceived without
severe
On Sat, Mar 19, 2016 at 7:49 AM, Sven R. Kunze wrote:
> On 18.03.2016 20:10, Palpandi wrote:
>>
>> You can do like this.
>>
>> if not my_iterable:
>>
>> for x in my_iterable:
>>
>
>
> Thanks for you help here, however as already pointed out, my_iterable is not
> necessarily a list but
alister wrote:
> On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
>> I'm kidding, of course. Keep it simple and use a flag like you would in
>> any other language:
>>
>> empty = True:
>> for item in items:
>> empty = False ...
>> if empty:
>> ...
>
> or even use the loop variabl
On Wed, Mar 16, 2016, at 13:01, Sven R. Kunze wrote:
> On 16.03.2016 17:56, Sven R. Kunze wrote:
> > On 16.03.2016 17:37, Random832 wrote:
> >> for item in collection:
> >> if good(item):
> >>thing = item
> >>break
> >> else:
> >> thing = default # or raise an exception, etc
On 16.03.2016 16:02, Tim Chase wrote:
On 2016-03-16 15:29, Sven R. Kunze wrote:
I would re-use the "for-else" for this. Everything I thought I
could make use of the "-else" clause, I was disappointed I couldn't.
Hmm...this must be a mind-set thing. I use the "else" clause with
for/while loops
On 16.03.2016 18:08, Random832 wrote:
Yeah, well, you can *almost* get there with:
try:
thing = next(item for item in collection if good(item))
except StopIteration:
thing = default
But the for/else thing seems like a more natural way to do it. Plus,
this is a toy example, if the body
On 2016-03-16 11:23, Sven R. Kunze wrote:
> for x in my_iterable:
> # do
> empty:
> # do something else
>
> What's the most Pythonic way of doing this?
If you can len() on it, then the obvious way is
if my_iterable:
for x in my_iterable:
do_something(x)
else:
somethin
On 16.03.2016 11:47, Peter Otten wrote:
What would you expect?
A keyword filling the missing functionality? Some Python magic, I
haven't seen before. ;-)
class Empty(Exception): pass
...
def check_empty(items):
... items = iter(items)
... try:
... yield next(items)
...
On 18.03.2016 20:10, Palpandi wrote:
You can do like this.
if not my_iterable:
for x in my_iterable:
Thanks for you help here, however as already pointed out, my_iterable is
not necessarily a list but more likely an exhaustible iterator/generator.
Best,
Sven
--
https://mail.pyth
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
On 16.03.2016 13:57, Peter Otten wrote:
I'd put that the other way round: syntactical support for every pattern
would make for a rather unwieldy language. You have to choose carefully, and
this requirement could easily be fulfilled by a function, first in your
personal toolbox, then in a public
Tim Chase wrote:
> On 2016-03-16 16:53, Peter Otten wrote:
>> > item=None
>> > for item in items:
>> > #do stuff
>> if item is None:
>> > #do something else
>>
>> I like that better now I see it.
>
> The only problem with that is if your iterable returns None as the
> last item
Sven R. Kunze wrote:
> On 16.03.2016 11:47, Peter Otten wrote:
>>
>> What would you expect?
>
> A keyword filling the missing functionality? Some Python magic, I
> haven't seen before. ;-)
>
>>
> class Empty(Exception): pass
>> ...
> def check_empty(items):
>> ... items = iter(items)
On 2016-03-16 15:29, Sven R. Kunze wrote:
> I would re-use the "for-else" for this. Everything I thought I
> could make use of the "-else" clause, I was disappointed I couldn't.
Hmm...this must be a mind-set thing. I use the "else" clause with
for/while loops fairly regularly and would be miffed
On 16.03.2016 17:37, Random832 wrote:
On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote:
I can imagine that. Could you describe the general use-case? From what I
know, "else" is executed when you don't "break" the loop. When is this
useful?
for item in collection:
if good(item):
t
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 "emp
On Wed, 16 Mar 2016 13:45:53 +, Mark Lawrence wrote:
> On 16/03/2016 13:25, alister wrote:
>> On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
>>
>>> Sven R. Kunze wrote:
>>>
Hi,
a colleague of mine (I write this mail because I am on the list) has
the following issue:
On 16.03.2016 15:29, Sven R. Kunze wrote:
On 16.03.2016 13:57, Peter Otten wrote:
I'd put that the other way round: syntactical support for every pattern
would make for a rather unwieldy language. You have to choose
carefully, and
this requirement could easily be fulfilled by a function, firs
On 16.03.2016 14:58, alister wrote:
no , i just typed it, while trying to hold a conversation with swmbo
:-( apologies to the op if e could not see where i was intending to go
with this.
No problem, I perform quite well at guessing folk's intention.
So, yes, I can extrapolate what you meant.
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 u
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?
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?
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
--
https://mail.python.org/mailman
On 3/16/2016 11:17 AM, Sven R. Kunze wrote:
On 16.03.2016 16:02, Tim Chase wrote:
Does it annoy me when I have to work in other languages that lack
Python's {for/while}/else functionality? You bet.
I can imagine that. Could you describe the general use-case? From what I
know, "else" is exec
On 2016-03-16 16:53, Peter Otten wrote:
> > item=None
> > for item in items:
> > #do stuff
> if item is None:
> > #do something else
>
> I like that better now I see it.
The only problem with that is if your iterable returns None as the
last item:
items = ["Something here", N
On 16/03/2016 13:25, alister wrote:
On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
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
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 doi
On Thu, 17 Mar 2016 05:05 am, Sven R. Kunze wrote:
> What I don't understand is why Python features "if break, then no else
> clause", but "if empty, then empty clause".
>
> I found this excellent post:
> https://shahriar.svbtle.com/pythons-else-clause-in-loops
That post describes the motivating
On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
> 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
On 16.03.2016 17:20, Terry Reedy wrote:
On 3/16/2016 11:17 AM, Sven R. Kunze wrote:
On 16.03.2016 16:02, Tim Chase wrote:
Does it annoy me when I have to work in other languages that lack
Python's {for/while}/else functionality? You bet.
I can imagine that. Could you describe the general
On 16.03.2016 17:56, Sven R. Kunze wrote:
On 16.03.2016 17:37, Random832 wrote:
On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote:
I can imagine that. Could you describe the general use-case? From
what I
know, "else" is executed when you don't "break" the loop. When is this
useful?
for ite
On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote:
> I can imagine that. Could you describe the general use-case? From what I
> know, "else" is executed when you don't "break" the loop. When is this
> useful?
for item in collection:
if good(item):
thing = item
break
else:
th
On 17.03.2016 01:27, Steven D'Aprano wrote:
That post describes the motivating use-case for the introduction
of "if...else", and why break skips the "else" clause:
for x in data:
if meets_condition(x):
break
else:
# raise error or do additional processing
It might help to r
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?
What would you expect?
>>> class Empty(Exc
On 16.03.2016 11:28, Joaquin Alzola wrote:
If len(my_iterable) is not 0:
for x in my_iterable:
# do
else:
# do something else
I am sorry, I should have been more precise here.
my_iterable is an iterator that's exhausted after a complete iteration
and cannot be restored.
I
You could do something like ...
If len(my_iterable) is not 0:
for x in my_iterable:
# do
else:
# do something else
There should be a more code efficient way to do this.
-Original Message-
From: Python-list
[mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Be
37 matches
Mail list logo