News for hackers. Stuff that matters.
Re:Pattern matching accepted for Python by enriquto on Tuesday February 09, 2021 @05:41PM (#26083440)
What's the problem with nested comprehensions? How else do you create bi-dimensional stuff?
Re:Pattern matching accepted for Python by magnusmundus on Tuesday February 09, 2021 @06:11PM (#26083771)
Not the same kind of nesting, is it?

  [[None for _y in range(y)] for _x in range(x)]
for two dimensions is fine, while

  [None for _x in range(x) for _y in range(y)]
(to create a flattened 2-dim list? excuse the dummy example) is just begging for later headache. Explicit looping is much more readable. Note the flipped order of expressions -- these two statements create the same iterators.

Not to mention, you can even reuse the name for extra evil:

  [i for i in range(2) for i in range(3)]
results in

  [0, 1, 2, 0, 1, 2]
Re:Pattern matching accepted for Python by joshuamorton on Tuesday February 09, 2021 @06:41PM (#26084055)
Fwiw, I find creating a 2d array and then flattening (`itertools.chain.from_iterable([[None for _y in range(y)] for _x in range(x)])` to be both readable and reasonably concise, though yes explicit loops are also fine.
Re:Pattern matching accepted for Python by cat199 on Tuesday February 09, 2021 @11:21PM (#26085724)
nice trick.. though if you are relying on the 'generator' aspect of comprehensions this may be problematic (will create in-memory copies)