[Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin

Chris Angelico rosuav at gmail.com
Wed Apr 11 10:48:19 EDT 2018


On Thu, Apr 12, 2018 at 12:37 AM, Peter O'Connor
<peter.ed.oconnor at gmail.com> wrote:
> Let's look at a task where there is "one obvious way"
>
> Suppose someone asks: "How can I build a list of squares of the first 100
> odd numbers [1, 9, 25, 49, ....] in Python?"  The answer is now obvious -
> few people would do this:
>
>     list_of_odd_squares = []
>     for i in range(100):
>         list_of_odd_squares.append((i*2+1)**2)
>
> or this:
>
>     def iter_odd_squares(n)):
>         for i in range(n):
>             yield (i*2+1)**2
>
>     list_of_odd_squares = list(iter_odd_squares(100))
>
> Because it's just more clean, compact, readable and "obvious" to do:
>
>     list_of_even_squares = [(i*2+1)**2 for i in range(100)]
>
> Maybe I'm being presumptuous, but I think most Python users would agree.
>

Or:

squares = [i**2 for i in range(1, 200, 2)]

So maybe even the obvious examples aren't quite as obvious as you might think.

ChrisA


More information about the Python-ideas mailing list