Python Forum
Beginner Q: failure to find string in list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Beginner Q: failure to find string in list (/thread-9032.html)



Beginner Q: failure to find string in list - Jenny - Mar-18-2018

I am a total beginner, so likely to be missing something obvious here. First post so be kind!

       print('Looking for ', Name_key, 'in', headerList)
       Name_listIndex = headerList.index(Name_key)
The print function acts out exactly as I expect, giving me confidence that my variables are correctly set:

>>> Looking for ['IAU Name'] in ['IAU Name', 'Designation', 'ID', 'Const.', '#', 'WDS_J', 'Vmag', 'RA(J2000)', 'Dec(J2000)', 'Approval Date']

Next I expected it to set Name_listIndex to match the index at which Name_key is found in headerList. So I was expecting it to set Name_listIndex = 0. Instead I get:

>>> ValueError: ['IAU Name'] is not in list

It works fine using a dummy list and key variable, just not with the real data. I am too confused.

Thanks for any support!
Jenny


RE: Beginner Q: failure to find string in list - snippsat - Mar-18-2018

You are looking for list ['IAU Name'],but in list it's a string 'IAU Name'.
>>> lst = ['IAU Name', 'Designation', 'ID', 'Const.', '#', 'WDS_J', 'Vmag', 'RA(J2000)', 'Dec(J2000)', 'Approval Date']
>>> name = ['IAU Name']
>>> if name in lst:
...     print('True')
...     lst.index(name)
...
     
>>> name = 'IAU Name'
>>> if name in lst:
...     print('True')
...     lst.index(name)
...     
True
0



RE: Beginner Q: failure to find string in list - Jenny - Mar-18-2018

Awesome thank you.