Case insenstive search of 2D list

65 views Asked by At

I need to search a list of lists for the phrase "ss".

The problem is, it could be capitalised in any way.

With a normal list, I know that I could create a for loop, and use str.lower()

For itertools, it's less simple. There's no .lower attribute. Here's the code so far:

return(ss in (itertools.chain.from_iterable(result))

Does anybody know a way that I can search this while ignoring case?

1

There are 1 answers

1
Moinuddin Quadri On BEST ANSWER

You may use any() in this case as:

my_ss_list = ["ss", "superseded"]
chained_list = [word.lower() for word in itertools.chain.from_iterable(result)] 
#                    ^ convert all the words to lower case

any(ss in chained_list for ss in my_ss_list)
# returns `True`/`False` based on the occurrence of any of the item
# of `my_ss_list` in `chained_list`

OR, you may explicitly make a check for each word without using any as:

"ss" in chained_list or "superseded" in chained_list