# \`for-while\` generator comprehension syntax

**URL:** <https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146>\
**Category:** Ideas\
**Created:** [September 19, 2023, 8:51pm UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146 "2023-09-19T20:51:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Red-exe-Engineer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/red-exe-engineer/32/14754_2.png) [@Red-exe-Engineer](https://discuss.python.org/u/Red-exe-Engineer)\
**Post date:** [September 19, 2023, 8:51pm UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/1 "2023-09-19T20:51:34Z")

</div>

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:

```python
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:

```python
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:

```python
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.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [September 19, 2023, 10:31pm UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/2 "2023-09-19T22:31:35Z")

</div>

From last year:

> [@Itertools.takewhile but in a list comprehension](https://discuss.python.org/t/itertools-takewhile-but-in-a-list-comprehension/21772):
>
> Would it make any sense to allow while in a list (or generator, more importantly) comprehension? For example, you can currently filter an entire list or generator like: [item for item in iterable if item \< 10] But suppose I wanted to do the equivalent of itertools.takewhile, that is consume elements until some condition is met. I feel like it’s succinctly summed up by: [item for item in iterable while item \< 10] without having to import this function. Given that this function exists already,…

---

<div class="post-metadata">

**Author:** ![Red-exe-Engineer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/red-exe-engineer/32/14754_2.png) [@Red-exe-Engineer](https://discuss.python.org/u/Red-exe-Engineer)\
**Post date:** [September 19, 2023, 11:36pm UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/3 "2023-09-19T23:36:43Z")

</div>

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](https://discuss.python.org/t/itertools-takewhile-but-in-a-list-comprehension/21772/6) comment has another syntax change that might be required for this suggestion:

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

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [September 20, 2023, 12:46am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/4 "2023-09-20T00:46:59Z")

</div>

> [@Red-exe-Engineer](#):
>
> Also, [this](https://discuss.python.org/t/itertools-takewhile-but-in-a-list-comprehension/21772/6) comment has another syntax change that might be required for this suggestion:

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

---

<div class="post-metadata">

**Author:** ![Red-exe-Engineer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/red-exe-engineer/32/14754_2.png) [@Red-exe-Engineer](https://discuss.python.org/u/Red-exe-Engineer)\
**Post date:** [September 20, 2023, 1:56am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/5 "2023-09-20T01:56:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 20, 2023, 2:06am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/6 "2023-09-20T02:06:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thejcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/thejcannon/32/23759_2.png) [@thejcannon](https://discuss.python.org/u/thejcannon)\
**Post date:** [September 20, 2023, 2:14am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/7 "2023-09-20T02:14:43Z")

</div>

Doesn’t `itertools.takewhile` do the trick?

> **[itertools — Functions creating iterators for efficient looping](https://docs.python.org/3/library/itertools.html#itertools.takewhile)**
>
> This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python. The module standardizes a core set...

---

<div class="post-metadata">

**Author:** ![Red-exe-Engineer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/red-exe-engineer/32/14754_2.png) [@Red-exe-Engineer](https://discuss.python.org/u/Red-exe-Engineer)\
**Post date:** [September 20, 2023, 2:15am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/8 "2023-09-20T02:15:29Z")

</div>

Ay, thank you! ;D

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

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 20, 2023, 2:15am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/9 "2023-09-20T02:15:33Z")

</div>

> [@thejcannon](#):
>
> Also, doesn’t `itertools.takewhile` do the trick?

See the end of the post 😉

---

<div class="post-metadata">

**Author:** ![thejcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/thejcannon/32/23759_2.png) [@thejcannon](https://discuss.python.org/u/thejcannon)\
**Post date:** [September 20, 2023, 2:16am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/10 "2023-09-20T02:16:22Z")

</div>

I need to stop replying when I’m tired 🤦‍♂️

Sorry for the noise

---

<div class="post-metadata">

**Author:** ![Red-exe-Engineer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/red-exe-engineer/32/14754_2.png) [@Red-exe-Engineer](https://discuss.python.org/u/Red-exe-Engineer)\
**Post date:** [September 20, 2023, 2:17am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/11 "2023-09-20T02:17:43Z")

</div>

It’s all good. xD

---

<div class="post-metadata">

**Author:** ![Red-exe-Engineer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/red-exe-engineer/32/14754_2.png) [@Red-exe-Engineer](https://discuss.python.org/u/Red-exe-Engineer)\
**Post date:** [September 20, 2023, 3:17am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/12 "2023-09-20T03:17:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [September 20, 2023, 5:35am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/13 "2023-09-20T05:35:14Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![RonnyPfannschmidt](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ronnypfannschmidt/32/1714_2.png) [@RonnyPfannschmidt](https://discuss.python.org/u/RonnyPfannschmidt)\
**Post date:** [September 20, 2023, 7:13am UTC](https://discuss.python.org/t/for-while-generator-comprehension-syntax/34146/14 "2023-09-20T07:13:45Z")

</div>

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?)
