Remember to check only index to index

Hi there,

Can someone can explain me how stop my code from double writing the following output: [0, 2, 3, 3]?
I understand why it happens that way, but I don’t know how to overcome it.

Code:

#Write your function here
def same_values(lst1, lst2):
  x = []
  for i in lst1:
    for j in lst2:
      if i == j:
        x.append(lst1.index(i))
        i += 1
  return x    
      
#Uncomment the line below when your function is done
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))

what if you run your code outside codecademy environment?

I’m sorry - now I see that my description of a problem is very misleading! I meant that I don’t know how to get rid of double 3 in my output. Of course, after running a code, I get just one line of text which is [0, 2, 3, 3] .

now i understand.

you understand that you only need to include values which are present in both list at the same index?

the problem with your if condition is that it doesn’t care about indexes.

the last item of lst1 (3) is also included, given it will equal the second to last item of lst2.

keep in mind that .index() method gives the first occurrence in the list

1 Like