On Sat, 5 Mar 2022 at 04:34, Christopher Barker <python...@gmail.com> wrote: > > >> > In the status quo, we have a filtered loop written as: >> > >> > for item in items: >> > if condition: >> > block > > > The thing is, if block is more than a couple lines, you need to look down to > see if there’s an else or any number of elif blocks down there— so this isn’t > a clear expression of a filtered iteration after all. Which is why I > suggested that: > > for item in items: > if not condition: > continue > block > > Would be the more direct expression of "filtered iteration" -- still not a > whole lot of code, but a bit more awkward. > > The other thing that I think is relevant is that comprehensions already have > a filtering option -- so while new syntax technically. this proposal would be > familiar and create a bit more consistency in the language. > > Another half formed idea: > > Comprehension can express map-like behavior or map and filter behavior, but > no way to filter without a do nothing map. > > e.g if you want to only filter, you need to do: > > (item for item in items if condition) > > I wonder if there's a way to not need to the "item for item" wasted text: > > (Item in items if condition) > and > [item in items if condition] > maybe?? > > it's not legal syntax now. That would then allow: >
Tempting, very tempting. I've been in the situation of wanting this before. Without the word "for", though, it doesn't say that it's a loop, and "item in items" is a condition, "if" can be used as part of an if/else expression, and overall, this is dangerously close to valid syntax. What if omitting the value were to automatically use the loop iterator? [for item in items if condition] But at this point it's not really saving much. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/OEM5FAKWTGLTRPLKUUHPQHRUDJ762IVE/ Code of Conduct: http://python.org/psf/codeofconduct/