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

8 Likes

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???

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

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

1 Like

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]:
      new_lst.append(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:

http://www.pythontutor.com/visualize.html#mode=edit

or a similar tool when facing such problems.

4 Likes

Crystal clear, thanks a lot!!

I got the output with this piece of code. My question is, if i can solve a problem using for and while loops, should i go for an extra mile and try to look for the solution using list comprehension.

def same_values(lst1,lst2):
  same = []
  i = 0
  j = 0
  while i < len(lst1):
    while j < len(lst2):
      if lst1[i] == lst2[j]:
        same.append(i)
      i += 1
      j += 1
  return same

You could, if you have an understanding of what is involved. Can we use while in a comprehension? That will be something to find out. We already know we can write nested loops in a comprehension, which is another thing for you to discover.

Bottom line, list comprehensions are a powerful and useful tool in a suitable setting. That doesn’t mean they are always the best tool for doing the job where readability and debugging are concerned.

Hi guys,
My code seem like leads me to infinity loop and I don’t know why. I need your helps #get-help :

#Write your function here
def same_values(lst1, lst2):
  index = 0
  newlist =[]
  while index < len(lst1):
    if lst1[index] == lst2[index]:
      newlist.append(index)
      index += 1
  return newlist


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

index only increases when the values are equal. So when the values are not equal, index won’t increase, and the the loop will just endlessly use the same non equal values.

1 Like

Thank you. Now I know I put the index +=1 in the wrong place. It work in this way:

#Write your function here
def same_values(lst1, lst2):
  index = 0
  newlist =[]
  while index < len(lst1):
    if lst1[index] == lst2[index]:
      newlist.append(index)
    index += 1
  return newlist


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

Thank you for asking this question. I had the same issue, and my code was set up the same way as yours.

hi,

I understand most of the elements in this code but I don’t seem to understand why we have to add a range to it. I’m very new to python, can someone explain this to me?

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

Welcome, @dev3111411212.

It’s okay to be honest with us. Did you skip over any lessons to get to this point? Don’t be shy.

No, I haven’t skipped any lessons. If this was explained earlier, I should have paid more attention.

That is a signal to go back and review. Don’t be playing catch up as you progress. Go back and touch up, refresh, review, revive your curiosity, explore and experiment. Don’t leave the rudiments and basics behind. Make them part of your every day.

1 Like

Hi everyone!

I wanted to know why I keep getting these curley brackets when I print instead of the square brackets for lists.

def same_values(lst1, lst2):

new_list_index =

for index in range(len(lst1)):

if lst1[index] == lst2[index]:

  new_list_index.append(index)

return set(new_list_index)

because you use set()? which gives you, well, a set (which is also a data type)