Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

if I have a list of variables [A, B, C, D]. How do I efficiently find which is None?

I have

if None in (A, B, C, D):

    ValueError("None found.")

But it only tests if None is inside it, and can not locate it. I can easily locate it using for loop, but please don't do this... What I need is

if None in (A, B, C, D):

    # Find where is None

    ValueError("None found in {?}.")

1 Answer

0 votes
by (25.1k points)

 If you want the index of the first None in a list x, you can do it like this:

x.index(None)

Or if you want a list of all the indexes of None you can do it like this:

[index for index, value in enumerate(x) if value is None]

Related questions

0 votes
1 answer
+1 vote
1 answer
asked Oct 30, 2019 in Python by humble gumble (19.4k points)
0 votes
1 answer
0 votes
1 answer
asked Mar 23, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer

Browse Categories

...