On 1/28/2013 8:33 AM, Wolfgang Maier wrote:
Dear all, I guess this is so obvious that someone must have suggested it before:
No one who understands comprehensions would suggest this.
in list comprehensions you can currently exclude items based on the if conditional, e.g.:
[n for n in range(1,1000) if n % 4 == 0]
Why not extend this filtering by allowing a while statement in addition to if, as in:
Why not? Because it is flat-out wrong. Or if you prefer, nonsensical. You want to break, not filter; and you are depending on the order of the items from the iterator. Comprehensions are a math-logic idea invented for (unordered) sets and borrowed by computer science and extended to sequences. However, sequences do not replace sets. https://en.wikipedia.org/wiki/Set-builder_notation https://en.wikipedia.org/wiki/List_comprehension Python has also extended the idea to dicts and iterators and uses almost exactly the same syntax for all 4 variations.
[n for n in range(1,1000) while n < 400]
This would translate as def _temp(): res = [] for n in range(1, 1000): while n < 400): res.append(n) return res _temp() which makes an infinite loop, not a truncated loop. What you actually want is res = [] for n in range(1, 1000): if >= 400): break res.append(n) which is not the form of a comprehension. -- Terry Jan Reedy