Python Code Challenges: Loops (Advanced) - #4 Same Values

Hi,

I have been running in to the same problem a few times now and can´t figure out why one solution works and another not.

In the question, attached Here, we are asked to compare 2 lists of numbers. If the number at the same indices i both list, we should return the index-number into a new list, which we will return to be able to print it.

Now to the question part - initially i tried to solve it as follows:

#Write your function here
def same_values(lst1, lst2):
  new_list = []
  
  for number in lst1:
    if lst1[number] == lst2[number]:
      new_list.append(number)
  return new_list

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

However, as you can see, when you try to run this code you get the error message that the list index is our of range. This is something I´ve encountered multiple times and after some thinking, I´ve realised that when running a for-loop in a list the first variable actually calls the value, it is not a counter as i C (which is the only language I´ve coded a tiny bit in before, CS50).

Instead i wrote a far uglier code, as can be seen below however working and meeting all cafeterias while levering my knowledge about the foor-loop. Code shown below, after comes the second part of the question.

#Write your function here
def same_values(lst1, lst2):
  new_list = []
  count = -1
  for number in lst1:
    count += 1
    if number == lst2[count]:
      new_list.append(count)
  return new_list

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

If we then look at the solution provided by Codecademy, attached below, the suggest using a random variable "index" which is fine and i understand. They later sett the "length"-variable or what you would want to call it to the length of one of the strings. Then what is interesting, they use the same code as i suggested first, however this time it works. Is the explanation as is as if we use the range-function it starts at 0 as our list-indices and then the range is set to the length of a list, i.e. it goes 0,1,2 and so on and therefore matches the index? 

thanks for helping!

def same_values(lst1, lst2):
new_lst =
for index in range(len(lst1)):
if lst1[index] == lst2[index]:
new_lst.append(index)
return new_lst```

1 Like

Addition - The reason why i am asking is that.

We know that “len” starts a 1 instead of 0 while range starts at 0 if we do not precise anything else.

Hence, in the above example - This should return a range of 0 trouch 5, i.e. one to much.

We should run in to some problem when our loop tries to run a 6th time (when index = 5) as there is no index 5 beacuse to my knowledge, when running for-loops, the last index of the range is included as well.

Please advise!

It won’t loop 6 times b/c remember that the range() function (start, stop, [step]) does not include the upper-bound argument (or value) of the range.
ex:

for i in range(8): 
    print(i, end=", ")
#prints: 0, 1, 2, 3, 4, 5, 6, 7,

See:
https://docs.python.org/3/library/stdtypes.html#range

and,
https://docs.python.org/3/library/functions.html#len

1 Like

Thank you!! Much much appreciated!

I will have a read trough them but then that´s where i went wrong, the range-function does not include the last index, hence no problem with the len-function being one higher as this is needed if we want to use the range function.

Thank you & have a great weekend!

1 Like