1

I have two lists

list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
list2 = ['01:15', 'abc', '01:15', 'uvz', '01:45', 'ghi' ]

and when I loop through the list

list_difference = []
for item in list1:
    if item not in list2:
        list_difference.append(item)

and I managed to get the difference, but I need time as well because it is a separate item and 'uvz' does not mean to me anything in the list with a few thousand entries. I tried to convert it to the dictionary, but it overwrites with the last key:value {'01:15' : 'def'}.

martineau
  • 119,623
  • 25
  • 170
  • 301
Levicki
  • 23
  • 4

2 Answers2

2

Convert the two lists to sets of tuples, then use the set difference operator.

set1 = set((list1[i], list1[i+1]) for i in range(0, len(list1), 2))
set2 = set((list2[i], list2[i+1]) for i in range(0, len(list2), 2))
list_difference = list(set1 - set2)
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

reformat your data, then do whatever you have done before

list1=list(zip(list1[::2],list1[1::2]))
list2=list(zip(list2[::2],list2[1::2]))
Bing Wang
  • 1,548
  • 1
  • 8
  • 7