6 ms·
I personally find list comprehensions in python pretty horrible. They seem to exist only to do lots of stuff in one single line of code. You end up with totall
by mattlondon 7y ago
I personally find list comprehensions in python pretty horrible.
They seem to exist only to do lots of stuff in one single line of code. You end up with totally impenetrable unreadable perl-esq garbage write-once-read-never code that is too clever for its own good. And people say python is easy to learn and good for beginners...!
A better approach would be something like Java Streams/.net Lync/RxX pattern IMO. Explicit, clear, no magic, logical.
- ledauphin 7y agoI personally find them extremely readable, and a lot clearer than for loops about the lack of side effects.
- BenFrantzDale 7y agoI disagree. It’s basically set-builder notation, which probably predates any programming language. It’s a concise way to express filtering.
- FpUser 7y ago" impenetrable unreadable perl-esq garbage write-once-read-never code " - I had about the same impression when I first saw it. I think ternary operator is as far as I am willing to go ;)
- novok 7y agoYou can decompose any list comprehension into it's equivalent for loop fairly easily. I don't see what is so magic about them other than the language having a special compiler level optimization for them? g = [i for i in list if x == 3] -> g = []; for i in list: if x == 3: g.append(i);
- mplanchard 7y agoI see people frequently say that list comprehensions are special-cased and highly optimized by the compiler, but I did a few experiments recently with `timeit` and found that using `map()` is on the order of 1.5x faster in all the cases I tested. I think that either winds up being faster than a manual for loop, though
- genidoi 7y agodic = {k: v for k, v in dic.items() if k in other_dic and v == "bar"} How could that be improved? That's 3-4 LOC minimum in any other language My main grip is python's ternary operators, since the True value is evaluated before the condition, if you are doing ternaries on things that might throw exceptions the False value has to come first value = 0 if key not in dic else dic[key] * 5 rather than (throws indexerror if key isn't in dic) value = dic[key] * 5 if key in dic else 0
- grzm 7y agodic = {k: v for k, v in dic.items() if k in other_dic and v == "bar"} > "How could that be improved? That's 3-4 LOC minimum in any other language" If I'm understanding the comprehension correctly, (into {} (filter (fn [[k v]] (and (get other-dic k) (= v "bar"))) dic)) Though, for readability, I'd likely write it as: (->> dic (filter (fn [[k v]] (and (get other-dic k) (= v "bar")))) (into {})) Legibility is in the eye of the beholder.
- mythrwy 7y agoList comprehensions are one of those abuse subject features (like most things) where light simple use is fantastic but some people just take it too far into unreadable nastiness.
- zzyzxd 7y agolist comprehensions can be elegant and actually improve readability. However, I do agree that it can be easily misused. I have seen many junior Python programmers writing super long, complicated comprehensions that hurt my brain. They think it is pretty cool just because their solutions are one-liners.