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

Question:

So basically I am trying to compare two lists to see if they hold the same value at the same index at any point. If they do I return the index, if they do not, I return -1.

When I had first done this as a test I was having no issues however adding in the text has made it more difficult and my main issue is with the if else statement. I seem to be able to only get one message to work, either yes or no, not both based on the case.

Asked By: user15346274

||

Answers:

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.

Answered By: ep742

You could loop over both lists like so:

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


def seqsearch(DS, IS):

    for index_1, element_1 in enumerate(DS):
        for index_2, element_2 in enumerate(IS):

            if (element_1 == element_2) and (index_1 == index_2):
                print(f"Yes! Found at index ={index_1}")
                return index_1
    print("No!")
    return -1


print(seqsearch(Dlist, Ilist))

However, there are more improvements you can make. zip() indeed is a better option, but slightly more complicated to understand.

Also, please note that return is not the same as print. You were not returning -1; you were printing it.

Answered By: Riya

i try use your code for better undrestand (but based of your use we have many option)

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


def seqsearch_with_print(DS, IS):
    """this function only print not return!!!
    if you use return your function ended!"""
    for i in range(len(DS) - 1):
        if DS[i] == IS[i]:
            print(f"Yes! Found at index {i}")
        else:
            print(f"No not equal in {i} index")


def seqsearch_with_list(DS, IS):
    """this function save history of (equal index or not: as 1, -1)"""
    temp_list = []
    for i in range(len(DS) - 1):
        if DS[i] == IS[i]:
            temp_list.append(1)
        else:
            temp_list.append(-1)
    return temp_list


def seqsearch_with_list_dict(DS, IS):
    """this function save history of (equal index or not: as 1, -1)
    as key, value (key== index, value==list item)"""
    temp_list = []
    for i in range(len(DS) - 1):
        if DS[i] == IS[i]:
            temp_list.append({i: 1})
        else:
            temp_list.append({i: -1})
    return temp_list


# no need print(in function we used print)
seqsearch_with_print(Dlist, Ilist)
"""
return of function: 
    No not equal in 0 index
    No not equal in 1 index
    Yes! Found at index 2
    No not equal in 3 index"""

# save history of search index in list
print(seqsearch_with_list(Dlist, Ilist))
"""
return of function with print:
    [-1, -1, 1, -1]
"""
# save history as dict key value
print(seqsearch_with_list_dict(Dlist, Ilist))
"""
return of function with print
    [{0: -1}, {1: -1}, {2: 1}, {3: -1}]
"""

Answered By: ali
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.