Home > Net >  How do I find the index of an unknown value in python?
How do I find the index of an unknown value in python?

Time:12-31

So basically I have a list like this -

[None,None,None,val]

and I don't know what val is. So how do I get the index of val (3) only knowing that it is not None

CodePudding user response:

This one line solution gives you the first value that is not None.

values = filter(lambda item: item is not None, mylist)
target_value = next(values)

print(target_value)

This returns the actual value, to get the index, look into this answer first comment which is:

next(filter(lambda item: item[1] is not None, enumerate(mylist)))[0]
  • Related