(meant to reply to the thread here so I'll send it again)
Thanks everyone for the input. I used a trivial example where the inside is
a single, simple expression (which can be refactored in many different,
clever ways) but the idea was that more complicated blocks could not be
(easily) refactored in similar ways. Additionally, there may be further
nested loops inside, each having their own conditions -- creating a
twice-nested overhead for each one, rapidly becoming gross.
This is "special" because it reduces the nesting overhead of conditioned
loops from 2 to 1, which is especially important if you've got several
nested loops:
for a in A:
if cond_a:
for b in B:
if cond_b:
# code block
then becomes:
for a in A if cond_a:
for b in B if cond_b:
# code block
you could even do:
for a, b in zip(A,B) if cond_a and cond_b:
# code block
Either option I feel is far more suitable than the first example.
If you'd like me to use a better demonstrative example, let me know.
On Fri, Apr 10, 2020 at 3:56 PM <[email protected]> wrote:
> André Roberge wrote:
> > On Fri, Apr 10, 2020 at 5:26 PM Elliott Dehnbostel
> [email protected]
> > wrote:
> > > Hello Everyone,
> > > If I've done this incorrectly, please let me know so that I can
> > > improve/revise. I'm new to the Python community and quite enjoy the
> more
> > > functional features of Python 3, but have I have a peeve about it. I'd
> like
> > > to propose and discuss the following enhancement to Python 3:
> > > Consider the following trivial for-loop:
> > > chars = "abcaaabkjzhbjacvb"
> > > seek = {'a','b','c'}
> > > count = 0for a in chars:
> > > if a in seek:
> > > count += 1
> > > Gross. Twice nested for a simple count.
> > > count = 0
> > > for a in chars:
> > > count = count + 1 if a in seek else count
> > > Once nested -- if nested == gross, then this is not gross. (However,
> I
> > prefer the twice nested which I find clear and simple -- not gross.)
> > André Roberge
> > >
> > > Python-ideas mailing list -- [email protected]
> > > To unsubscribe send an email to [email protected]
> > > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > > Message archived at
> > >
> https://mail.python.org/archives/list/[email protected]/message/A2PTKI.
> ..
> > > Code of Conduct: http://python.org/psf/codeofconduct/
> > >
>
> Nothing wrong with
> ```
> for a in chars:
> if a in seek:
> count += 1
> ```
> It is readable and does the job. There are other approaches that can do
> the job too.
>
> For instance,
>
> ```
> len([char for char in chars if char in seek])
> ```
>
> So why do we need a new syntax for this case? What does this new syntax so
> special?
> _______________________________________________
> Python-ideas mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/EGDK5ELS2KACDKQQ5T7MHWTKLFMM3KPQ/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/IU5OSHSZUZ36B4W7THPNZ5M23GXXK4FE/
Code of Conduct: http://python.org/psf/codeofconduct/