[Solved] Check to see if two lists have the same value at the same index, if so return the index. If not return -1

Introduction

Solution


I think a cleaner answer uses the built-in enumerate and zip functions:

Dlist = [17,13,10,6,2]
Ilist = [5,9,10,15,18]

def seqsearch(DS,IS):
    for idx, (d, s) in enumerate(zip(DS, IS)):
        if d == s:
            return f"Yes! Found at index = {idx}"

    return "No!\n-1"


print(seqsearch(Dlist,Ilist))

It’s unclear whether you want to return just the first index, or all the indices with matching elements. In either case, it is probably better if you just return the desired value and then add any formatting and print statements outside of the function’s scope.

3