Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I find the index of the next not NaN element in a python list

so I have the list:

['string_one', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 'string_two', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 'string_three', nan, nan, nan, 
nan, nan, nan, nan, nan, nan, nan, nan, 'string_four', nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, "string_five", nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]

this list is in the def with parametr that equals to one of the strings from the list. I need to find index of the next string

upd:
I am using pandas and nan came from the excel table. I got the right answer, please stop downvote me 😉

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are many ways to do it, here is one of them:

my_list = ['x', None, None, 'y', None] 
for index, elem in enumerate(my_list):
    if elem is not None:
        print(index)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading