4 ms·
That is somewhat annoying, yes. I'm a huge fan of Python's list comprehension, but it's generally accepted to be a normal part of the language, and IMO, more re
by sgarland 2y ago
That is somewhat annoying, yes. I'm a huge fan of Python's list comprehension, but it's generally accepted to be a normal part of the language, and IMO, more readable:
print([x for x in range(10) if not x % 2])
[0, 2, 4, 6, 8]
vs.
l = []
for x in range(10):
if not x % 2:
l.append(x)
print(l)
[0, 2, 4, 6, 8]
- psychoslave 2y agoWith ruby 3.4 (prior to that `_1` should be used instead of `it`): (0..10).select{it.even?}
- psychoslave 2y agoAlso not that the result is `[0, 2, 4, 6, 8, 10]`, unlike the behavior of Python which exclude the value explicitely passed in parameter from the represented range.
- okanat 2y agoCall me crazy, but I find the second example more readable. The first example is long line and you have to go back and forth to understand it. The second example is a single top down pass.
- crazygringo 2y agoAgreed, me as well. The second example reflects how my brain thinks and the intuitive order of what has to happen. In the first example, it's all out of order.
- sgarland 2y agoI’m sure I’m biased, since I code mainly in Python. List/dict comprehensions read the same to me as the longer form, just more concisely. This can be taken too far, and you can wind up with horribly obtuse one-liners that are just awful.
- okanat 2y agoI write and maintain code in a bunch of languages. Rust has risen to the first place, C is the close second followed by C++ and Python. All of the languages except C have similar iterator-based stuff that let you write one liners and often with lambdas. I dislike them all. They give way too much leeway and encourage many developers to try to prove how clever they are. Once you have to debug the code containing them, all of the complexity of the syntactic sugar comes crashing down on you. The debugger starts jumping to weird places, sometimes even optimized out parts of the standard libraries while for loops usually stay debugable.
- floxy 2y agox = np.arange(0,10,1) print(x[x%2==0])
- f33d5173 2y agoI've seen people complain about wrapping a comprehension over multiple lines, but I can't imagine packing them all in one. In your example, I would spread it out over five lines, so it would be at as long as the imperative case. While I like python, I think the unusual order that components of a comprehension are written is to its detriment. It would make more sense if they were in the same order in both examples. The python ternary has a similar issue.
- glandium 2y agoYou chose the one example that doesn't need a list comprehension. print(list(range(0, 10, 2)))
- sgarland 2y agoGood call.