Python Forum
Beginner Q: failure to find string in list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Q: failure to find string in list
#1
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
Reply
#2
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
Reply
#3
Awesome thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failure to run source command middlestudent 2 689 Sep-22-2023, 01:21 PM
Last Post: buran
  Program to find Mode of a list PythonBoy 6 1,065 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Beginner: Code not work when longer list raiviscoding 2 815 May-19-2023, 11:19 AM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,181 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find (each) element from a list in a file tester_V 3 1,204 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Dickey Fuller failure Led_Zeppelin 4 2,606 Sep-15-2022, 09:07 PM
Last Post: Led_Zeppelin
  read a text file, find all integers, append to list oldtrafford 12 3,512 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Find and Replace numbers in String giddyhead 2 1,219 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  find some word in text list file and a bit change to them RolanRoll 3 1,517 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 1,994 May-31-2022, 01:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020