Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There are people who write Python code like that, but it's an extreme minority. Here's the more likely way:

    array = [1, 0, 2, 3]
    new_array = [x * 2 for x in array
                 if x != 0]
Just as a matter of style, few Python programmers will use lambda outside something like this:

    array = [...]
    arry.sort(key=lambda ...)


i have always felt the backwards nature of list comprehensions makes them very hard to read


me too. Its one of the things that I kinda dislike in Python.


I guess you're right, list/generator comprehensions are the idiomatic way to filter and map in python, with the caveat of needing to have it all in a single expression (the same goes for lambda, actually).

I still feel like chained methods are easier to read/understand, but list comprehensions aren't that bad.


Even in Rust I don't like chains that go beyond ~4 operations. At some point it becomes clearer when expressed as a loop.


> with the caveat of needing to have it all in a single expression (the same goes for lambda, actually).

one could use multiple expressions in lambda in (modern) Python


Do you mean using the walrus operator? Because unless I missed a recent PEP, I don't know of a way to do this without something hacky like that.


yes

x = 1 y = 2

q = list(map(lambda t: ( tx := tx, ty := ty, tx+ty )[-1], [1, 2, 3]))

print(q)


Guido van Possum has expressed distaste for list comprehension. Take that for what it's worth.

https://news.ycombinator.com/item?id=13296280


I realize that "Guido van Possum" was almost certainly a typo here, but it _does_ make for an amusing mental image. I wonder what other forest creatures might have built programming languages? C++ built by Björn "the bear" Stroustrup? C# built by Anders Honeybadger? Ruby by Yukihiro "Catz" Cat-sumoto?


Indeed an autocorrect. But it was definitely Graydon Boar who created Rust


From the link, it sounds like GvR has expressed distaste for functional programming idioms like map/reduce, but not for list comprehensions.

At least it's not Go's "You gonna write a for loop or what?"


He was also against having any kind of lambda, and the one line version was the concession he was willing to let in.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: