4 ms·
I love Python comprehensions. I want to dislike Python - significant whitespace, weak lambdas, other trivial complaints - but the comprehensions are so nice, es
by Pete_D 8y ago
I love Python comprehensions. I want to dislike Python - significant whitespace, weak lambdas, other trivial complaints - but the comprehensions are so nice, especially combined with yield and the iteration protocol, that it's usually the first language I turn to. There's syntax for sets, dicts, and generators too:
>>> { char.upper() for char in "hello" }
set(['H', 'E', 'L', 'O'])
>>> { char: ord(char) for char in "world" }
{'d': 100, 'r': 114, 'o': 111, 'w': 119, 'l': 108}
>>> (char for char in "foobar").next()
'f'
- Tijdreiziger 8y agoWhat I really dislike about comprehensions is that the for syntax comes at the _end_. They would be much easier to parse if it came at the beginning.
- kjeetgill 8y agoI used to feel that way too. When you write out nested for loops the work is at the other end! For what it's worth, it is meant to resemble mathematical set builder notation. I think that familiarity turned me around on it (pun intended). From that perspective it feels more natural. [0]:https://en.m.wikipedia.org/wiki/Set-builder_notation https://en.m.wikipedia.org/wiki/Set-builder_notation