Temporary variables in list comprehensions

Tim Chase python.list at tim.thechases.com
Mon Jan 9 21:37:15 EST 2017

On 2017-01-09 13:16, Paul Rubin wrote:
> Tim Chase <python.list at tim.thechases.com> writes:
> >> result = [(tmp, tmp+1) for tmp in map(expensive_calculation,
> >> data)]
> >
> > As charmingly expressive as map() is, the wildly different
> > behavior in py3 (it's a generator that evaluates lazily) vs py2
> > (it consumes the entire iterable in one go) leads me to avoid it
> > in general,
> Well, there's itertools.imap which maps lazily in py2.

Yes, but it's one of those things that I have to remember to
distinguish which one I use based on whether I'm working in py2 or
py3.  Meanwhile, using a generator expression is readable (my #1
reason for using Python is its readability) and works across 2 and 3
without changes or thinking about it further.  So that's what I tend
to use.


More information about the Python-list mailing list