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

Finding minimum tuple value satisfying a given condition

I have some tuples: (0,1,2), (3,4,4), (2,2,2) and I would like to find the minimum of the the zero index such that the second index equals 2. I have tried this using the min built-in function but it is giving me a syntax error.

min(data, key=lambda x: x if x[2] == 2)

>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

Don’t use the key parameter. If you want to ignore certain elements in finding the minimum, use filter() instead:

data = [(0,1,2), (3,4,4), (2,2,2)]

print(min(filter(lambda x: x[2] == 2, data)))

This outputs:

(0, 1, 2)
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