I am looping through an array of objects, calling a method on each like so:
for cell in cells:
cell.update_type(next_cells[cell.index])
Is there a way to do the equivalent with map()?
It appears update_type
returns None
, so you could use:
any(cell.update_type(next_cells[cell.index]) for cell in cells)
but unless there is a problem with a normal loop, just stick with that. It's the most readable and you shouldn't optimize prematurely.
You shouldn't use map
here because there is no way to avoid using it on a Python function / lambda expression, so you won't get a speed advantage over a normal loop.
You shouldn't use a list comprehension because you're needlessly accumulating a list of the return values of update_type
even though you're ignoring them -- use any
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With