I have these two lists:
list_1 = ['less', 'more', 'more', 'more', 'more', 'more', 'more', 'more']
list_2 = ['less', 'less', 'less', 'more', 'less', 'less', 'more', 'less']
I want to find the index in list_1 if the corresponding element in the other list is the same. For example list_1[0] = list_2[0] , 0 would be the first element of the index list I am looking for but list_1[1] != list_2[1], so 1 would not be in my list
CodePudding user response:
You can first zip
them, then check two by two and with enumerate
save index like below. (as I ask and you say two list have same size)
Try this:
>>> list_1 = ['less', 'more', 'more', 'more', 'more', 'more', 'more', 'more']
>>> list_2 = ['less', 'less', 'less', 'more', 'less', 'less', 'more', 'less']
>>> [idx for idx, (a,b) in enumerate(zip(list_1, list_2)) if a==b]
[0, 3, 6]
# for more details
>>> list(zip(list_1, list_2))
[('less', 'less'),
('more', 'less'),
('more', 'less'),
('more', 'more'),
('more', 'less'),
('more', 'less'),
('more', 'more'),
('more', 'less')]
CodePudding user response:
you can use
[i for i in range(len(list_1)) if list_1[i]==list_2[i]]
if the lists are of the same length
out:
[0, 3, 6]