Python Forum

Full Version: Beginner Q: failure to find string in list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
Awesome thank you.