-1

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

sr0812
  • 324
  • 1
  • 9

1 Answers1

0

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]
Ali Hejazizo
  • 198
  • 5
  • 2
    OP wants the index not the item itself, so `next(filter(lambda item: item[1] is not None, enumerate(mylist)))[0]` seems more apt. –  Dec 30 '21 at 06:58
  • 1
    Yes. Didn't see that part. Will edit the response so that they look into your comment. – Ali Hejazizo Dec 30 '21 at 07:01