Is this #Python too compact? Perhaps, but there could be useful things to learn. Full explanation unpacked at nedbatchelder.com/blog/201802/...
52 likes 11 replies
Replies
馃殌 Brian Okken 路 Dec 12
This would be a fun (and slightly evil, if that鈥檚 different) live coding interview question.
David Buchanan 路 Dec 12
besides the point, but TIL you can use `list` in annotations as opposed to `List` (since 3.9)
Bruno Alla 路 Dec 12
Despite 14 years of writing Python professionally, I steer away from nested comprehension because I never remember which one is executed first... I love how your formatting hints at the actual order of execution, that's great. Would be nice if it was the default formatting from black/ruff.
Niko F枚hr 路 Dec 12
I would say that I don't care what it looks like if it has good unit tests. This kind of functions I'll always treat like a black box anyway :D
Quasar 路 Dec 12
Well I didn鈥檛 expect to learn anything, but partition?! How have I never heard of that!
Reinder 路 Dec 12
Certainly interesting and great article!! although I'm not so fond of multi-line list-comprehensions as imho it's not as clear as a normal loop.
sarthak sidhant 路 Dec 12
hi ned whats some micropython advice you have
Adam Silkey 路 Dec 12
I don't think it's too compact. Comprehensions are really fast, so they're good to use. I agree with what @browniebroke.com said about how you formatted the comprehension to make it more readable. And if this were checked into prod code, I'd probably have explanatory comments in the code/commit.
Rodrigo Gir茫o Serr茫o 馃悕馃殌 路 Dec 12
I was trying to rejig things to do the `a, _, b` assignment to the partition results elsewhere, perhaps with a walrus, but I failed spectacularly. Do you have any other renditions of this function with different assignments?
Rodrigo Gir茫o Serr茫o 馃悕馃殌 路 Dec 12
For extra compactness and lessons for readers, use `chain.from_iterable` instead of the last loop 馃榿 def expand(s): return chain.from_iterable( range(int(a), int(b or a) + 1) for p in s.split(",") for a, _, b in [p.partition("-")] )
Ivan Karabadzhak 路 Dec 13
It鈥檚 a tricky, but a nice one!