2

I am trying to pass arguments in to a function which is being mapped. Is there a way to clean this up or optimize?

# Map resized images
resized_imgs = tuple(map(resize_image, all_img_steps, [None for img in all_img_steps], [output_height for img in all_img_steps]))

Thanks!

David Alford
  • 1,109
  • 7
  • 23
  • 1
    Closely related: [How to do multiple arguments to map function where one remains the same in python?](https://stackoverflow.com/q/10834960/4518341) I would say it's a duplicate, but there's no such thing as a tuple comprehension so it's not quite identical. – wjandrea Oct 22 '20 at 00:15

1 Answers1

5

Use a generator expression instead of a map.

resized_imgs = tuple(resize_image(img, None, output_height) for img in all_img_steps)
wjandrea
  • 16,334
  • 5
  • 30
  • 53
  • Ahh, thank you! That is much more [readable](https://www.python.org/dev/peps/pep-0020/#id2). Learn something new every day! – David Alford Oct 21 '20 at 23:55
  • 2
    @DavidAlford Yes, comprehensions are more readable, that is basically [what Guido said about `map` as well](https://developers.slashdot.org/story/13/08/25/2115204/interviews-guido-van-rossum-answers-your-questions) ;) Great minds think alike – zvone Oct 22 '20 at 00:02