Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What happens after getting max value of map object in Python

nums = map(int, input().split())
if max(nums)** 2 == next(nums)**2 + next(nums)**2:
  print(True)

Hi. I found out that when next(nums) is executed, stopIteration is raised.
I thought only the max value of nums would be removed from nums after max(nums) was executed.
But are all the values of nums removed after max function is executed? Looking forward to help! Thanks

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

All values are removed, since you are running through the entire iterator (all values) to find the max value. Which then removes all the values.

Ex:

>>> it = iter([1, 2, 3])
>>> max(it)
3
>>> next(it)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    next(it)
StopIteration
>>> 

The reason for this is because in order to find the maximum value, you need to loop through and use all the values in the iterator.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading