0

I want to create a program, such that it returns the index of the element, if each element in list 1 matches the corresponding element in list 2

for example: [5, 1, -10, 3, 3], [5, 10, -10, 3, 5]. Here 5 in list 1 matches the first element 5 in list 2 hence it returns index 0. similarly -10 matches with -10. hence gives index 2.

required output [0,2,3]

MY CODE:

def same_values(lst1,lst2):
    n = 1
    lst_of_index = []
    while(n <= len(lst1)):
        for i in lst1:
            value_1 = i
        for j in lst2:
            value_2 = j
        if (value_1 == value_2):
            indexed_value = lst1.index(i)
            lst_of_index.append(indexed_value)
            n += 1
    return lst_of_index
      

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

when i run the code, it doesn't display anything. Is something wrong with my code?

Abhinav K
  • 57
  • 8
  • 1
    Seems like there is an infinite loop. So yes, there is something wrong with your code. – mkrieger1 May 03 '21 at 14:03
  • Use a debugger to find out *what* is wrong with your code. – mkrieger1 May 03 '21 at 14:05
  • 1
    Alternatively, add `print(value_1, value_2)` before `if (value_1 == value_2)` and watch. – mkrieger1 May 03 '21 at 14:06
  • 1
    Does this answer your question? [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – mkrieger1 May 03 '21 at 14:07
  • 1
    Also check out `enumerate`. See https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops – mkrieger1 May 03 '21 at 14:08
  • AsFarAsICanSee The quickest fix would be simply indent the second for loop by one level and the if bock by two levels – TheEagle May 05 '21 at 14:32
  • @Abhinav K, thank you for accepting my answer. But I believe that the answer by JohnSG is better. So, I think that you should mark his answer as correct. – user12137152 May 07 '21 at 04:12

3 Answers3

3

Combining the answers from @user12137152 and @diego-mourino with a list comprehension, you get a relatively simple one liner:

def same_values(lst1,lst2):
    return [idx for idx, val in enumerate(zip(lst1, lst2)) if val[0] == val[1]]
JonSG
  • 10,542
  • 2
  • 25
  • 36
2

The function you implementate has a major bug. The function doesn´t return anything because you are generating an infinite loop. That`s because you first iterate over all the values of the lists and, after that, you compare value_1 and value_2. In this example, the value_1 and value_2 will be 3 and 5 respectively (because you are comparing the last value of the lists because the if statement is after the two iterators over the lists at the same indentation level). And because of that, the while loop gets stuck.

I propose to you an alternative implementation, using enumerate() instead of a the while. When you use enumerate over a list, this iterator give you 2 elements: the position of a given element of the list (i) and the element of the list (in this case, value_1). This implementation is shown below:

def same_values(lst1,lst2):
    lst_of_index = []
    for i, value_1 in enumerate(lst1):
        value_2 = lst2[i]
        if (value_1 == value_2):
            lst_of_index.append(i)
    return lst_of_index
2

Use a for loop and zip to iterate over both the lists at the same time. The benefit of the following program is that it doesn't crashes even if the lists are of different sizes.

def same_values(lst1, lst2):

    lst_of_index = []
    n = 0
    for i, j in zip(lst1, lst2):
        if i == j:
            lst_of_index.append(n)
        n += 1
    return lst_of_index
      
print(same_values([5, 1, -10, 3, 3], [5, 10, -10, 3, 5]))
user12137152
  • 732
  • 1
  • 5
  • 14
  • If you `enumerate()` the `zip()` you will not need `n`. It becomes even simpler if you use a comprehension. – JonSG May 03 '21 at 15:52
  • Yup. You are correct. There goes your upvote. – user12137152 May 03 '21 at 19:36
  • Sorry, I thought I did upvote. done now :-) – JonSG May 03 '21 at 19:39
  • 1
    I am sorry. Probably because of my poor english, I think you got the wrong message. I wasn't asking you to upvote my answer. What I wished to convey was that I liked your answer and that's why I had upvoted it. I have edited my answer for you to remove your upvote if you feel so. – user12137152 May 03 '21 at 20:00