So basically I have a list like this –
JavaScript
x
2
1
[None,None,None,val]
2
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.
JavaScript
1
5
1
values = filter(lambda item: item is not None, mylist)
2
target_value = next(values)
3
4
print(target_value)
5
This returns the actual value, to get the index, look into this answer first comment which is:
JavaScript
1
2
1
next(filter(lambda item: item[1] is not None, enumerate(mylist)))[0]
2