The expected value is beyond the index?


Why does the code checker expect to see [0, 2, 3, 5] (which is beyond the index of the lists) and say that my code yielded [0, 2, 3, 1]? when the console prints out [0, 2, 3]?

much appreciated

The lists you pass argument to the function call is different from the lesson.

if you would use the same lists, you would indeed see [0, 2, 3, 1] returned by the function while it should be [0, 2, 3, 5]

your function should work for multiple lists (which the exercise is testing)

I think it’s because you’re using the “index()” method to append to the “lst”.

The “index()” method takes a single argument, the value of the element (not the index) that is to be searched for. It then compares the value to each element in the list starting at index 0 and increments until it finds the first match. It then returns the index of the match.

In your code example, the code checker used a set of lists that vary slightly from the lists in the function call in your code. The code checker lists have six elements.

print(same_values([5,1,-10,3,3,1], [5, 10, -10, 3, 5,1]))

In this case the the elements at index 5 match. But because the “index()” method uses the value of the list item (in this case 1) it searched for the value 1 and matched the value of the element at index 1 of “lst1”. So it returned the index position of 1.

I think the confusion is because all of the other matches are the same as what you would expect if the code was working correctly. But this is only because 1 is the only number that both matches and repeats in a list with the first instance not in the match position.

Here is another example with your cude using a third list with another element that repeats and appears before the match.

def same_values(lst1, lst2):
    lst = []
    for i, j in zip(lst1, lst2):
        if i == j:
            lst.append(lst1.index(i))
            print(lst1.index(i))
    return lst
print("Test one: \n")    
print(same_values([5,1,-10,3,3], [5, 10, -10, 3, 5]))
print("\n\n\nTest two: \n")        
print(same_values([5,1,-10,3,3,1], [5, 10, -10, 3, 5,1]))
print("\n\n\nTest three: \n")    
print(same_values([5,1,55,-10,3,3,1,55], [5, 10, -10, 3, 5,1,4,55]))


Output

Test one: 

0
2
3
[0, 2, 3]



Test two: 

0
2
3
1
[0, 2, 3, 1]



Test three: 

0
2
[0, 2]

It the case of the last function call the list elements match at index 0 (value of 5) and index of 7 (value of 55) but 55 is found in 'lst1" using “index()” and returns 2 as the index of the first instance.

Hope this helps. :smile: