0

Let the two lists be

x = [0,1,2,2,5,2,1,0,1,2]
y = [0,1,3,2,1,4,1,3,1,2]

How to find the similar elements in these two lists in python and print them. What I am doing-

for i, j in x, y:
   if x[i] == y[j]:
      print(x[i], y[j])

I want to find elements like x[0], y[0] and x[1], y[1] etc. This does not work, I am new to python. I want to find the exact index at which the elements are common. I want to find the index 0, 1, 3, 6, 8, 9; because the elements at these indices are equal

maya
  • 21
  • 3

2 Answers2

0
x = [0,1,2,2,5,2,1,0,1,2]
y = [0,1,3,2,1,4,1,3,1,2]

for i in range(len(x)):
  if x[i] == y[i]:
    print(i)

Note: if the lists are not in the same len, this will produce index out of bound error

shahaf
  • 4,750
  • 2
  • 29
  • 32
  • 1
    @maya, no it's not, are you using python3? – shahaf Apr 14 '18 at 10:54
  • Could you write it in the form of for loops(decompressed) – maya Apr 14 '18 at 10:57
  • It would also not work for the case, for i in x, y: , right? – maya Apr 14 '18 at 11:07
  • "TypeError: list indices must be integers or slices, not list" is the error – maya Apr 14 '18 at 11:08
  • @maya `for i, j in list` require ti list elements to be separate to two values i.e list = [[a,b],[c,d]...]` so `zip(list1,list2)` will return that type of list as for the error you can iterate over the shorter list like `range(min(len(x),len(y)))` – shahaf Apr 14 '18 at 11:21
0

I want to find the exact index at which the elements are common.

enumerate the zipped sequence:

>>> x = [0,1,2,2,5,2,1,0,1,2]
>>> y = [0,1,3,2,1,4,1,3,1,2]
>>> [i for i, (a, b) in enumerate(zip(x, y)) if a == b]
[0, 1, 3, 6, 8, 9]

zip(x, y) will give you pairs of elements from x and y at the same index:

>>> zip(x, y)
[(0, 0), (1, 1), (2, 3), (2, 2), (5, 1), (2, 4), (1, 1), (0, 3), (1, 1), (2, 2)]

Enumerating this sequence gives you the index of each item:

>>> list(enumerate(zip(x, y)))
[(0, (0, 0)), (1, (1, 1)), (2, (2, 3)), (3, (2, 2)), (4, (5, 1)), (5, (2, 4)), (6, (1, 1)), (7, (0, 3)), (8, (1, 1)), (9, (2, 2))]

As you can see, the elements have a (int, (int, int)) structure. These tuples are unpacked with the for i, (x, y) in ... syntax in the list comprehension. Finally, we only care about the ones where the (int, int) part holds the same integer at both positions (if a == b) and out of these matches, we only take the index i.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Thank you for your response, could you tell me where was the problem in my code..the simplified version – maya Apr 14 '18 at 10:56
  • @maya `for i, j in x, y:` does not work because you are implictly trying the assignments `i, j = [0, 1, 2, 2, 5, 2, 1, 0, 1, 2]` and `i, j = [0, 1, 3, 2, 1, 4, 1, 3, 1, 2]` where the right hand side has too many values to unpack into two names `i` and `j`. – timgeb Apr 14 '18 at 10:58
  • So, if I write nested for loops, it would still not keep track of the exact sequence as it would loose the indices, is it possible to do this without zip? – maya Apr 14 '18 at 11:04
  • @maya "is it possible to do this without zip" <- yes, with `for index in range(len(x)) ...` but this is considered bad style. – timgeb Apr 14 '18 at 11:05
  • Ok, thanks. And for i in x,y: gives an error "TypeError: list indices must be integers or slices, not list", even though i= [0, 1, 2, 2, 5, 2, 1, 0, 1, 2] and j = [0, 1, 3, 2, 1, 4, 1, 3, 1, 2], why is that. Is it because i can't take two values, and if we created a tuple instead it would store the values? – maya Apr 14 '18 at 11:14
  • @maya it's time to stop the follow up questions now. We usually deal with one question per topic here. – timgeb Apr 14 '18 at 11:18