What is an index?

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

1 Like

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

When using “range(len(lst1))” it will give an index range for the length of the first list. These numbers are not using the values in that particular index for that list. This means that by having this number set you can compare both lists with the index number, further finding values at that given index. Having another list length would be considered inefficient, as this is a comparison of values with the same indexes.

If you still don’t quite understand what I mean I will supply my code with annotations

Hi, I’m just getting familiar with all these. I was playing around to understand the logic behind this loop and by changing the order of the elements in lst2 and giving a specific index value for lst1 in the “if” statement, I noticed that the index value referenced is from lst2. And know suddenly I don’t understand how this particular loop works (I am not having any problem with understanding the rest of the challenges).

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

print(same_values([5, 1, -10, 3, 3], [3, 5, 10, -10, 5]))  #Prints 0 (index value of number 3 in lst2, instead of printing lets say 3 or 4, which are index values from lst1.

Does it make any sense what I am asking? Why the reference index appended to new_lst is from lst2 instead of lst1?

lst1[4] is the 5th element (index 4), which has a value of 3, agree?

so then you compare each value of lst2 with 3, so:

3 == 3 
3 == 5
3 == 10
3 == -10
3 == 5

the only time this condition is true is the first iteration of the loop, when index=0, so 0 gets appended to new_list.

i can really recommend something like this:


or a similar tool when facing such problems.

Crystal clear, thanks a lot!!