Searching the index of multiple items in a list

I have a list

myList = ["what is your name", "Hi, how are you", "What about you", "How about a coffee", "How are you"] 

Now I want to search the index of all occurrences of "How" and "what" . How can I do this on Pythonic?

+5
source share
2 answers

Sounds like a one-line Python capable of doing!

 [i for i, j in enumerate(myList) if 'how' in j.lower() or 'what' in j.lower()] 
+3
source

This will work, but assumes you don't need case sensitivity:

 myList = ["what is your name","Hi, how are you","What about you","How about a coffee","How are you"] duplicate = "how are you" index_list_of_duplicate = [i for i,j in enumerate(myList) if duplicate in j.lower()] print index_list_of_duplicate [1,4] 
+1
source

All Articles