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()?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It appears
update_typereturnsNone, so you could use: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
maphere 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_typeeven though you’re ignoring them — useanyinstead.