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

Check if substring exists in a list containing booleans?

I want to check if a substring exists in a list. But i can’t iterate through it because of it having booleans. I’m trying to convert booleans to str(booleans) but is that necessary? I’m sure there must be another efficient way without changing data
error

def replace_falseTrue_as_strings(lst):
    for i in lst:
        if isinstance(i,list):
            for j in i:
                if all(not isinstance(j,list)):
                    for k in j:
                        k_new.append(str(k))
                    return k
                if any(isinstance(j,list)):
                    return replace_falseTrue_as_strings(j)
        else:
            i_new.append(str(i))
            return j_new

>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

You could add a typecheck:

any( (isinstance(x, str) and 'abl' in x) for x in some_list)

in case of x not being a string (a boolean here), isinstance(x, str) is False and the second member is not checked, this avoids the TypeError.

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