Conditional iteration

A

at

I would like to spark the discussion about the following syntax problem I
encounter.

THE PROBLEM

I have a lot times the following code:

for x in [-2, -1, 0, 1, 2, 3, 4]:
if x > 0:
... more code...


It is not the addional line containing 'if x > 0:' that bothers me, but the
additional indentation.


THE SOLUTION

More pythonic in view would be:

for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0:
... more code ...


This blends basically

[x for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0]

and

x = y if x > 0 else 10


EXTENDING

And maybe a few usefull variants, like:

for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0 else -x:
... more code ...

In this case x will be 2, 1, 0, 1, 2, 3, 4.
 
G

Giovanni Bajo

at said:
THE PROBLEM

I have a lot times the following code:

for x in [-2, -1, 0, 1, 2, 3, 4]:
if x > 0:
... more code...


It is not the addional line containing 'if x > 0:' that bothers me, but the
additional indentation.

for x in ...:
if not x > 0:
continue

... more code ...
 
R

Roberto Bonvallet

at said:
More pythonic in view would be:

for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0:
... more code ...

Pythonic? Do you realize that Python hasn't even adopted well-known
statements like 'switch' and 'do while' because they would be redundant?

This could be more convenient to you, but certainly not pythonic.
Cheers,
 
N

Neil Cerutti

at said:
More pythonic in view would be:

for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0:
... more code ...

Pythonic? Do you realize that Python hasn't even adopted
well-known statements like 'switch' and 'do while' because they
would be redundant?

This could be more convenient to you, but certainly not
pythonic. Cheers,

I tried it once myself. It seemed like a feasible thing that
might work in Python. It didn't annoy me that it didn't work, but
it did seem natural to me given the syntax of comprehensions.
 
P

Paul Rubin

at said:
I have a lot times the following code:

for x in [-2, -1, 0, 1, 2, 3, 4]:
if x > 0:
... more code...

Use:

for x in (x in [-2, -1, 0, 1, 2, 3, 4] if x > 0):
... more code ...
 
C

Chris Mellon

at said:
I have a lot times the following code:

for x in [-2, -1, 0, 1, 2, 3, 4]:
if x > 0:
... more code...

Use:

for x in (x in [-2, -1, 0, 1, 2, 3, 4] if x > 0):
... more code ...
--

or filter:

from itertools import ifilter

for x in ifilter(lambda x: x > 0, [-2, -1, 0, 1, 2, 3, 4]):
...more code...
 
A

at

You proposal, seems nice to me but it doesn't work with Python 2.4.3, should
it work with 2.5?

Again I am just wondering if the approach for

[x for c x in some_list if some_condition]

and
x = a if b else c

could be generalized for normal straight forward iterations:

for x in some_list if some_condition:

--- etc...

I am not looking for a work around but more interest if other people might
judge this syntax would come in handy?

Interested in any feedback!

Kind regards,

@






Paul said:
at said:
I have a lot times the following code:

for x in [-2, -1, 0, 1, 2, 3, 4]:
if x > 0:
... more code...

Use:

for x in (x in [-2, -1, 0, 1, 2, 3, 4] if x > 0):
... more code ...
 
A

at

Forget 'pythonic'.

I just need to get work done and I see this type of conditional iteration
showing up many times obscuring my code because of the additional
indentation.

In line with previous syntax improvements made in Python my proposal (or
obvious variants) seems a logical next step. Unless of course nobody
appreciates it. That's the discussion I'd like to have here in the forum.

All the best
@



Roberto said:
at said:
More pythonic in view would be:

for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0:
... more code ...

Pythonic? Do you realize that Python hasn't even adopted well-known
statements like 'switch' and 'do while' because they would be redundant?

This could be more convenient to you, but certainly not pythonic.
Cheers,
 
G

Gabriel Genellina

I just need to get work done and I see this type of conditional iteration
showing up many times obscuring my code because of the additional
indentation.

Me too. When I don't like the additional indentation I usually have:
if not condition: continue
at the beginning of the block
In line with previous syntax improvements made in Python my proposal (or
obvious variants) seems a logical next step. Unless of course nobody
appreciates it. That's the discussion I'd like to have here in the forum.

Looks good to me.
 
S

shandy.b

The proposed solution impairs readability because there's a "surprise"
at the end. List comprehensions already open the language up to
readability abuse. Lets not add more.

To avoid the unwanted indentation, I would go with the already
suggested "if not x>0: continue" solution or else something like this:

positiveMembers = [x for x in [-2, -1, 0, 1, 2, 3, 4] if x>0]
for x in positiveMembers:
#do stuff

This has the advantage of being more self-documenting.
 
C

Carl Banks

at said:
I am not looking for a work around but more interest if other people might
judge this syntax would come in handy?

Of course people have expressed interest in this in the past, but it's
not going to happen. There's a way to nest for and if statements, and
a different way to nest for and if clauses in listcomps, and the two
methods are considered distinct. Although Guido has said that saving
indentation levels is important, he hasn't said anything (that I'm
aware of) that suggests it's important enough to add this complexity
and redundancy to the language. Sorry.


Carl Banks
 
A

at

No offense, but my conclusions from your mail is that readability is a
matter of taste.

My brains need to process a whole lot more information with your solution
than in my proposal...

but I read somewhere else that GvR rejected the proposal :-(

Ciao,
@


The proposed solution impairs readability because there's a "surprise"
at the end. List comprehensions already open the language up to
readability abuse. Lets not add more.

To avoid the unwanted indentation, I would go with the already
suggested "if not x>0: continue" solution or else something like this:

positiveMembers = [x for x in [-2, -1, 0, 1, 2, 3, 4] if x>0]
for x in positiveMembers:
#do stuff

This has the advantage of being more self-documenting.
I would like to spark the discussion about the following syntax problem I
encounter.

THE PROBLEM

I have a lot times the following code:

for x in [-2, -1, 0, 1, 2, 3, 4]:
if x > 0:
... more code...


It is not the addional line containing 'if x > 0:' that bothers me, but
the additional indentation.


THE SOLUTION

More pythonic in view would be:

for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0:
... more code ...


This blends basically

[x for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0]

and

x = y if x > 0 else 10


EXTENDING

And maybe a few usefull variants, like:

for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0 else -x:
... more code ...

In this case x will be 2, 1, 0, 1, 2, 3, 4.
 
A

at

Dear Carl,

Well, all I can say that for me as a user it would make sense...

Curiosity: in what sense is it redundant?
All solution/workarounds I have seen so far involve creation of new lists
(subsets) adding to more processing/computation/memory usage. Redundant
suggests that you know alternatives that don't do that.

Does Guido ever change his mind?

Cheers,

@
 
C

Carl Banks

at said:
Well, all I can say that for me as a user it would make sense...

Which is, like, step one out of a hundred for getting a syntax change
into the language.
Curiosity: in what sense is it redundant?

It creates syntactical support for two different ways to do something.
If your plan were adopted, then we'd have two different spellings for
the same thing:

for i in a:
if i != 0:
use(i)

for i in a if i != 0:
use(i)

Now, redundant syntax isn't a deal breaker by itself. You have to ask
what is buys you. In this case, all it does is save you a single level
of indentation--that's it. There's no performance benefit. It doesn't
simplify logic. It doesn't make the code any more readable of clear.
It's only a minor improvement in conciseness. It hardly saves any
typing (unless you indent by hand). Even its one clear benefit, saving
indentation, is something you can already get with "if not x:
continue".

Considering how little this syntax change buys, it really doesn't make
a lot of sense for a language that places high emphasis on avoiding
redundancy.

All solution/workarounds I have seen so far involve creation of new lists
(subsets) adding to more processing/computation/memory usage. Redundant
suggests that you know alternatives that don't do that.

Does Guido ever change his mind?

Yes, but I guarantee "it makes sense for me" isn't going to convince
him. By the way, I'd suggest when posting to comp.lang.python and/or
python-list in the future, you put your replies beneath the quoted text
for the benefit of any future readers (not to mention present readers).


Carl Banks
 
P

Paul Rubin

at said:
You proposal, seems nice to me but it doesn't work with Python 2.4.3, should
it work with 2.5?

Again I am just wondering if the approach for

[x for c x in some_list if some_condition]

and
x = a if b else c

could be generalized for normal straight forward iterations:

for x in some_list if some_condition:

Sorry, I got it wrong. Should have said:

for x in (x for x in some_list if some_condition):
...

So your example would have been:

for x in (x for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0):
... more code ...

It should work in 2.4.
 
G

greg

at said:
It is not the addional line containing 'if x > 0:' that bothers me, but the
additional indentation.

I don't find the additional indentation bothersome.
In fact I think it's helpful, because it makes it
obvious that there is something else going on besides
just a loop.
 
A

at

My comments below.

Kind regards,
@


Carl said:
Which is, like, step one out of a hundred for getting a syntax change
into the language.


It creates syntactical support for two different ways to do something.
If your plan were adopted, then we'd have two different spellings for
the same thing:

for i in a:
if i != 0:
use(i)

for i in a if i != 0:
use(i)

With the current Python syntax, I can create for every two lines of code a
dozen alternative implementations:

# example 1
a = {}
a['b'] = 'c'

versus:
a = {'b': 'c'}


# example 2
l = []
for l in some_list:
if some_condition:
l.append(l)

versus:
l = []
for x in some_list:
if some_condition:
l = l + [x]

or:
l = [x for x in some_list if some_condition]

(the beautiful one)

So your argument doesn't mean much I would say!
Now, redundant syntax isn't a deal breaker by itself. You have to ask
what is buys you. In this case, all it does is save you a single level
of indentation--that's it. There's no performance benefit. It doesn't
simplify logic. It doesn't make the code any more readable of clear.
It's only a minor improvement in conciseness. It hardly saves any
typing (unless you indent by hand). Even its one clear benefit, saving
indentation, is something you can already get with "if not x:
continue".


Well there is a clear performance benefit, or more precisely a productivity
benefit. And -please- do not underestimate this for a language like Python,
which has many supporters due to its perceived and high productivity and
challenged on this point by languages like Ruby.

'for x in some_list if some_condition:'

is psychological very strong, because the brain will most likely treat the
in the same way as :

for every apple in the fruitbasket take one if green


Basically upon reading this first line you know exactly to what list of
items your next section of code applies.

But again everything is a matter of taste and I assume that's why the change
control body is put into the hand of one person.


Considering how little this syntax change buys, it really doesn't make
a lot of sense for a language that places high emphasis on avoiding
redundancy.



Yes, but I guarantee "it makes sense for me" isn't going to convince
him. By the way, I'd suggest when posting to comp.lang.python and/or
python-list in the future, you put your replies beneath the quoted text
for the benefit of any future readers (not to mention present readers).
I hope this this thread will support the "it makes sense for me" with
arguments. Again it is not my intention to fight windmills, but to see if
there are strong arguments against it on one hand and if there supporters
on the other hand.
 
A

at

Thanx Paul!

Do you know if this generates a new list internally (memory consumption?)

@


Paul said:
at said:
You proposal, seems nice to me but it doesn't work with Python 2.4.3,
should it work with 2.5?

Again I am just wondering if the approach for

[x for c x in some_list if some_condition]

and
x = a if b else c

could be generalized for normal straight forward iterations:

for x in some_list if some_condition:

Sorry, I got it wrong. Should have said:

for x in (x for x in some_list if some_condition):
...

So your example would have been:

for x in (x for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0):
... more code ...

It should work in 2.4.
 
A

at

Hi Greg,

Well point is that the condition is the only thing happening and does not
really apply to the indented code section, but basically to the list used
in the indented code section.

When I read this code back its like, 'oh we use this list' and by the if
some_condition the next thing I think is 'hm, not all of the list' and even
worse should I worry if more restrictions can be found when I read
further...

All the best,

@
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top