-1

I'm sure this is already out there somewhere but I cannot find it. So I have two list:

sheet_names = ['T307710_TOOLS', 'T307710_CSYS'] #This is pulled in from wb.get_sheet_names()

look_for = ['tools', 'Tools', 'TOOLS', 'setup', 'Setup', 'SETUP'] #these are the terms I am looking for in the sheet names

I want to compare the two and then pull the 'T307710_TOOLS' name and position and then store that in a variable tool_sheet.

This is my attempt at it, I am not sure how to compare the two list in the nested for-loop:

#multiple sheet names, only 1 will specify either TOOLS or SETUP
sheet_names = ['T307710_TOOLS', 'T307710_CSYS'] #wb.get_sheet_names() in my code
#the sheet name I am looking for can be anything from T3771_TOOLS to setupsheet
look_for = ['tools', 'Tools', 'TOOLS', 'setup', 'Setup', 'SETUP']

#me attempting to iterate through the sheet names
for i in sheet_names:
    for j in look_for:
        if j is in i:
            tool_sheet = i
smichael_44
  • 167
  • 11
  • in python, you just use `in` rather than `is in`, so if you change your final if statement to read `if j in i` then that should do what you're after? – Emi OB Apr 13 '22 at 14:00
  • @EmiOB, lol, yeah, that worked exactly. I'm new to python and was sitting here for like 20 minutes asking myself how this wasn't working. Thank you. – smichael_44 Apr 13 '22 at 14:04
  • 1
    You could shorten `look_for` to only include lower case *words* then when making a comparison with a sheet name use the [.lower()](https://docs.python.org/3/library/stdtypes.html#str.lower) method ... `if j in i.lower():`.. – wwii Apr 13 '22 at 14:07
  • Just use sets for this. – Charlie Clark Apr 14 '22 at 08:29

1 Answers1

0
for i in sheet_names:
    for j in look_for:
        if j in i:
            tool_sheet = i

Difference was "is in" vs "in" in for-loop

smichael_44
  • 167
  • 11