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

Is there a Python function that checks if any list element is of a specific data type?

For example: I have a list called container. In it are some elements and I want to check if eg. any of them are complex.

container = [1, 0.5, "text", 1j]    
if isinstance(container[?], complex):
    print("This list has complex elements")

In this example, I could’ve written container[-1], but is there an universal method?
I could use a for loop, but I’m wondering if there are better ways to do this.

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 :

There are many ways to apply something to entire container, e.g.

any(isinstance(element, complex) for element in container)
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