FAQ: Code Challenge: Loops - Same Values


This community-built FAQ covers the “Same Values” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Same Values

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


explain this breifly.


there isn’t much to explain? This is a topic where you can ask questions related to this pro exercise:



so index means the elements in the list, right?
Also, didnt seem to quite grasp the output. Explanation needed.


that is not what index/indices means.

the elements of a list are just called elements or values.

The index(es)/indices of a list is the position of an element/value in the list


what really confuses me is : “for index in range(len(lst1)):
if lst1[index] == lst2[index]:” so in the first line “index” represents that it would iterate through every element in lst1, that’s fine. But how can it iterate through the lst2 when there was no distinct declaration like: “for i in the range(len(lst2)):” sometimes you need to have two variables called for 2 lists, for example the exponent challenge exercise, we had to call 2 variables to work that out, but in this case my code:
def same_values(lst1,lst2):
for i in range(0,(len(lst1))):
for j in range(0,(len(lst2))):
if lst1[i]==lst2[j]:
didnt work because i didn’t call for the same variable. now i’m confused how to understand this iteration behavior of python. can anyone kindly tell me what’s it, that i’m doing wrong???


You need to do something like this. You don’t have loop before the if statement. Hence, only one value will return. And obvisouly lst1 and lst1 are already defined; you don’t need to create the for cause for the loop to get the list. In sum, first, check the index in each list. second, seek if the value are met by check list [index]. Third, if match, append them into a new list

def same_values(lst1, lst2):
index =

for i in range (len(lst1)):

for i in range (len(lst2)):

for i in range (len(lst2)):
if lst1[i] == lst2[i]:
index.append (i)
return index


I had trouble with this initially because I indented the return with the first for loop.
it returned [0, 2, 3, 0, 2, 3, 0, 2, 3, 0, 2, 3, 0, 2, 3] because it repeated the command from the second for loop for the 5 index positions in lst1, correct?


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:
    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]))


Test one: 

[0, 2, 3]

Test two: 

[0, 2, 3, 1]

Test three: 

[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:

#Write your function here
def same_values(lst1,lst2):
  lst3 = []
  for index in range(len(lst1)):
    if lst1[index] == lst2[index]:
  return lst3

#Uncomment the line below when your function is done
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))

Ok so Im quite confused here. Why do we only have to say “for index in range(len(lst1))”. How come we are not also looking at the range for the second list. Can someone explain to me what the loop is actually doing?


given the lists are of the same length, the indices for both lists are the same. Range just produces a list, this list just contains integers. These integers just so happen to match the indexes we need, so we can use these integers from the range list, to lookup values by index in both lst1 and lst2


why isn’t this working :frowning:

#Write your function here
def same_values(lst1, lst2):
  lst3 = []
  for i1 in lst1:
    for i2 in lst2:
      if i1==i2:
  return lst3
#Uncomment the line below when your function is done
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))


you nested the loops, as a result, for each value in lst1, you will loop completely over lst2

this can result in problems when you have the same value multiple times in a list. Lets stick with this example:

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

5 is a good example, 5 is twice in lst2, given how your code is currently written, for both fives in lst2, we will append the index of the 5 in lst2 to lst3

furthermore, the .index() method will give the first element it can find. This will result in more problems

do you understand the assignment? You only need to add the index to the result list (lst3) when the values at the same index of both lists is the same


idk but i always keep thinking of stupid complex solution which never work, when the solution was pretty simple and straight forward, i just created a for loop using range(len(lst)) and placed my if statement to compare the two lists inside the loop.


that is the challenge, to come up with a simple solution. Which is indeed difficult. Keep practicing, it will get easier.


thanks a ton for the help :smile:


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.


#Write your function here
def same_values(lst1, lst2):
  x = []
  for i in lst1:
    for j in lst2:
      if i == j:
        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?