Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two lists keeping duplicates but removing same items in indexed positions in Python

I have two lists:

List1 = ['One', 'Two', 'Three', 'Five', 'Seven', 'Ten', ' Two', 'One'] 
List2 = ['Nine', 'Two', 'Seven', 'Five' , 'Five', 'Three',  'One', 'One']

The lists are of the same size.

What I want is, to match list1 with list2 by indexes and remove the matching items in list2 if the corresponding indexed item is the same. Otherwise it shouldn’t remove the items. Duplicates can exist in non indexed positions in the Newlist2.

This is what I expect:

List1:#Same as the previous

NewList2 = ['Nine', 'Seven', 'Five', 'Three', 'One']

like image 572
null_override Avatar asked Mar 02 '23 11:03

null_override


1 Answers

NewList2 = [ y for (x, y) in zip(List1, List2) if x != y ] 
like image 84
ilyankou Avatar answered Mar 05 '23 19:03

ilyankou