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

Why does max() in this case return 7?

l = [7, 9, 5, 6, 13, 5, 15, 3]
t = max(l, key=lambda g:g if g%9==1 else 0)

t is the maximum element of l, whose remainder of the division by 9 is 1.

In this case, t equals 7. But this list doesn’t have any elements whose remainder of the division by 9 is 1 at all! Why doesn’t t equal 0?

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

>Solution :

The lambda calculates the key for each value to be 0, and so with all elements having an equivalent key, the first element is returned.

The lambda is simply used to calculate the value for comparison, it does not convert the value that is returned.

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