FAQ: Code Challenge: Loops - Same Values


#1

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

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!


#2

explain this breifly.


#3

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

http://codecademy.com/paths-redirect/5b5001a11d176d7acd8b2bf8/192b1c5a7b20eb1345e2914df7f66519/exercises/same-index


#4

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


#5

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


#6

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):
indice_lst=[0]
for i in range(0,(len(lst1))):
for j in range(0,(len(lst2))):
if lst1[i]==lst2[j]:
indice_lst.append(i)
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???


#7

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

for i in range (len(lst2)):
lst2

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


#8

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?


#9


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


#10

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)


#11

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:


#12
#Write your function here
def same_values(lst1,lst2):
  lst3 = []
  for index in range(len(lst1)):
    if lst1[index] == lst2[index]:
      lst3.append(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?


#13

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