# Conditional loop filtering

**URL:** <https://discuss.python.org/t/conditional-loop-filtering/27317>\
**Category:** Ideas\
**Created:** [June 1, 2023, 11:56am UTC](https://discuss.python.org/t/conditional-loop-filtering/27317 "2023-06-01T11:56:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![b11c](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/b11c/32/2350_2.png) [@b11c](https://discuss.python.org/u/b11c)\
**Post date:** [June 1, 2023, 11:56am UTC](https://discuss.python.org/t/conditional-loop-filtering/27317/1 "2023-06-01T11:56:47Z")

</div>

Python comprehension expressions have filtering built in:

```auto
[thing.foo for thing in bunch_of_things if thing.bar > 42]

```

But in regular loops we need to do this:

```auto
for thing in bunch_of_things:
    if thing.bar > 42:
        do_something_with(thing.foo)

```

It would be nice to have the same type of filtering in the loops as well:

```auto
for thing in bunch_of_things if thing.bar > 42:
    do_something_with(thing.foo)

```

Any thoughts?

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [June 1, 2023, 12:05pm UTC](https://discuss.python.org/t/conditional-loop-filtering/27317/2 "2023-06-01T12:05:21Z")

</div>

See my proposal for a similar syntax for “filtered” assignment and “filtered” returns [Conditional Expresion/Statements](https://discuss.python.org/t/conditional-expresion-statements/25193)

And see this proposal to have “filtered” context managers: [Conditional context manager (with foo() if bar)](https://discuss.python.org/t/conditional-context-manager-with-foo-if-bar/27156)

I guess one of the answers you are going to get is that you can also do

```python
for thing in (y for y in bunch_of_things if y.bar > 42):
    do_something_with(thing.foo)

```

which of course it is not convoluted at all (`filter` can also be applied here with a `lambda`)

Whereas I would tend to see a slowly emerging pattern … people want to have things conditionally executed with an `if` statement at the end.

My 2 cents.

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [June 1, 2023, 12:29pm UTC](https://discuss.python.org/t/conditional-loop-filtering/27317/3 "2023-06-01T12:29:03Z")

</div>

Previous discussions:

- [Allow comprehension syntax in loop header of for loop](https://discuss.python.org/t/allow-comprehension-syntax-in-loop-header-of-for-loop/25864)
- [A "for ... if" statement](https://discuss.python.org/t/a-for-if-statement/15086)

---

<div class="post-metadata">

**Author:** ![b11c](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/b11c/32/2350_2.png) [@b11c](https://discuss.python.org/u/b11c)\
**Post date:** [June 1, 2023, 12:41pm UTC](https://discuss.python.org/t/conditional-loop-filtering/27317/4 "2023-06-01T12:41:07Z")

</div>

Yes, I am aware of all these options, and still think that the approach I outlined is the most viable.

1. It already exists as syntax (in comprehensions).
2. There is some similarities to the “conditional statements” proposal, but I think that that one opens quite a can of edge cases.
3. It’s orthogonal to the context manager proposal.
4. It’s much simpler than either the comprehension or filter options, which are the current workarounds.

> Whereas I would tend to see a slowly emerging pattern … people want to have things conditionally executed with an `if` statement at the end.

This proposal is strictly about in-loop filtering, not any arbitrary execution. It is also in line with existing syntax of comrehensions (which were originally modelled after loops).

---

<div class="post-metadata">

**Author:** ![b11c](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/b11c/32/2350_2.png) [@b11c](https://discuss.python.org/u/b11c)\
**Post date:** [June 1, 2023, 12:43pm UTC](https://discuss.python.org/t/conditional-loop-filtering/27317/5 "2023-06-01T12:43:06Z")

</div>

> [@effigies](#):
>
> Previous discussions:

Thank you Chris, I missed those. 🙏

---

<div class="post-metadata">

**Author:** ![b11c](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/b11c/32/2350_2.png) [@b11c](https://discuss.python.org/u/b11c)\
**Post date:** [June 1, 2023, 2:34pm UTC](https://discuss.python.org/t/conditional-loop-filtering/27317/6 "2023-06-01T14:34:59Z")

</div>

OK, reading through these threads I realise that the damage has already been done by allowing `if` expressions to have different syntax in comprehensions from the basic loops. So we can’t implement this idea without breaking backward compatibility, even taking into account that `if..else` is extremely rarely used in `for` loops.
