Skip to content Skip to sidebar Skip to footer

Python Two Lists Finding Index Value

listEx = ['cat *(select: 'Brown')*', 'dog', 'turtle', 'apple'] listEx2 = ['hampter',' bird', 'monkey', 'banana', 'cat'] for j in listEx2: for i in listEx: if j in i:

Solution 1:

Just use enumerate:

listEx = ['cat *(select: "Brown")*', 'dog', 'turtle', 'apple']
listEx2 = ['hampter',' bird', 'monkey', 'banana', 'cat']

for j in listEx2:
    forpos, i in enumerate(listEx):
        if j in i:
            print j, "found in", i, "at position", pos, "of listEx"

This will print

cat found incat *(select: "Brown")* at position 0 of listEx

Solution 2:

Your problem is that you wrote j instead of i in the last line:

for j in listEx2:for i in listEx:if j in i:printlistEx.index(i)#                              ^ here

However, a better approach is to use enumerate:

foritem2inlistEx2:
    fori, iteminenumerate(listEx):
        ifitem2initem:
            printi

Post a Comment for "Python Two Lists Finding Index Value"