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 a value inside a list

I am trying to create a boolean result if a specific value is inside the following list. Unfortunately i am dealing with some error which i dont know how to fix it

list = [array(['63977', '63981', '91916', '63908', '97158', '63906', '63910',
        '63956', '63970', '63969', 'Other'], dtype='<U5')]

For example i was trying to find value 63977 and my try was :

if '63977' in list:
    print('find it')

but i got the following error:

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

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

>Solution :

You have created nested list so that code is not doing what you think it is.
You are actually checking if specific element is in outer array but instead you should search if element exists in inner array.

Try next:

from numpy import array

list = [array(['63977', '63981', '91916', '63908', '97158', '63906', '63910',
        '63956', '63970', '63969', 'Other'], dtype='<U5')]

if '63977' in list[0]:
    print('find it')

If you create list like this:

list = array(['63977', '63981', '91916', '63908', '97158', '63906', '63910',
        '63956', '63970', '63969', 'Other'], dtype='<U5')

Then your code would work:

if '63977' in list:
    print('find it')

Hope that helps.

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