Skip to content
Advertisement

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

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

Advertisement

Answer

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]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement