3 ms·
I'm sometimes tempted to use it in loops, when for x in map(func, iterable): ... is cleaner than for x in (func(y) for y in iterable): ... a
by duckerude 8y ago
I'm sometimes tempted to use it in loops, when
for x in map(func, iterable):
...
is cleaner than
for x in (func(y) for y in iterable):
...
and shorter than
for y in iterable:
x = func(y)
...
But that mostly only makes sense if you already have a suitable named function (e.g. str), and I usually end up going with the third variant anyway.
There's an analogous case with filter and `if not cond: continue`.
map and filter calls are trivially rewritten to generator expressions, but it's not necessarily an improvement.