`for-while` generator comprehension syntax

Yesterday I came up with a very interesting syntax idea, after chatting about it with some friends I’ve decided to ask whether or not it can be implemented. Obviously I’m probably going to do something wrong, but I hope my post gets me on the right track.

The point of my syntax idea would be to allow breaking and finishing a generator expression if a condition is met, here’s an example:

items = [i for i in range(10) while i < 5]
print(items) # [0, 1, 2, 3, 4]

That example could probably be done with if i < 5, however here’s another one that gives a better idea of what this might be able to do:

items = [1, 2, 3, 4, "5", 6,  7, 8, 9]

var = [i for i in items if isinstance(i, int)]
print(var) # [1, 2, 3, 4, 6, 7, 8, 9]

var = [i for i in items while isinstance(i, int)]
print(var) # [1, 2, 3, 4]

The expanded version of this generator might look something like this:

items = [1, 2, 3, 4, "5", 6, 7, 8, 9]
var = []

for i in items:
    if not isinstance(i, int):
        break

    var.append(i)

I’m aware of itertools.takewhile, I just think it might be better to have this as a syntax feature instead.

2 Likes

From last year:

3 Likes

Thanks for the reply! I guess I didn’t search hard enough before asking, my bad.

Even though that post didn’t seem to going anywhere, I still want to see this kind of feature get implemented. I’d do it myself, but I’m honestly not that great at C and hardly understand Python’s source code structure.

Also, this comment has another syntax change that might be required for this suggestion:

[item for item in iterable while item < 10]
# <==>
for item in iterable while item < 10:
	yield item

And that comment clearly states that this is NOT expected to garner any support. Which it didn’t.

I actually like the idea though, it might not seem useful but that doesn’t mean it’s useless. Being able to iterate over something and immediately break if a condition is met sounds like a great idea to me, and apparently Don Patterson thought so too, so it’s obviously something at least two people would try to use.

The comments of the original post seem to suggest that comprehensions are for performance, I think a for-while syntax could be done in a way that doesn’t bottle things down much.

I’d also like to note that the syntax just looks pretty and compact, which while may not be Python’s main goal is definitely a bonus.

You’re free to think it’s not a good idea, but I think I’m gonna try to implement it myself and run some tests.

It’s a fun idea and might make some things nicer to express, but the bar for adding new syntax is very high. So if you’re proposing to add it to Python, you’ll get pushback from people. There has to be sufficient evidence that it’s worth adding.

But it’s a fun idea to play around with if you want to experiment with changing syntax.

2 Likes

Doesn’t itertools.takewhile do the trick?

1 Like

Ay, thank you! ;D

I’ve been wanting to get into syntax modifications for awhile now as I’ve had a few ideas before.

See the end of the post :wink:

2 Likes

I need to stop replying when I’m tired :man_facepalming:

Sorry for the noise

3 Likes

It’s all good. xD

1 Like

I got Python 1.13.0a0 compiling on my laptop, which was surprisingly easy considering my history of debugging compiling errors.

On to the testing!

EDIT: So… the code is uh… I’m probably in over my head. xD
Not gonna stop me from trying random stuff until something works.

1 Like

for (with optional if) for building collections was added because it mimics standard rule-based set builder notation in math and some other languages. It is fairly frequently used. Other syntax combinations have been proposed - and rejected. They either try to do too much in one statement or are rarely needed or are already available in itertools.

my understanding is, that with iterators as is, the code would have to consume/drop one mismatching element

as such this would be a language element that by design would have to drop data unless a surrounding api was added (peekable iterators?)

1 Like