Why my code is not working fine?

My loop is getting too large. Because of this, result returned wrong.

def same_values(lst1, lst2):
    new_list = []
    for i in range(len(lst1)):
        for j in range(len(lst2)):
            if(lst1[i] == lst2[j]):
                new_list.append(i)
    return new_list
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))

result return as

[0, 0, 2, 3, 4]

code exercise link :https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-loops-cc/exercises/same-index

new_list.append(i)
You don’t want to append the index that your outer loop is on to the list, you want to append lst1[i], the element of the list

1 Like