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

How do I check the first index of an array of arrays

Say I have a value…

x = 5

and a numpy array of arrays which looks something like…

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

arr = [[1,2], [3,4], [5,6]]

how would I check the first index of each array (ie. 1, 3, 5) to see if they = x?

>Solution :

You may find any() function useful in this case. It takes a list as an argument, and returns True if any of their arguments evaluate to True (and False otherwise).
Here’s how you do

if any([i[0] == x for i in arr]):
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