Home > Back-end >  How to compare two unordered list and print the unorder number
How to compare two unordered list and print the unorder number

Time:11-15

I have two unordered list

x = [1, 2, 3, 4, 5]
b = [1, 1, 2, 3, 4, 5]

I want to compare two list and print the new item added to list b

after comparing two list

I need to get the value 1 from the above list

I tried compare and sort libraries, but didn't found the right solution, any help would be appreciated

CodePudding user response:

A nice use of Counter here, subtract the occurence of x to the ones of b and keep the keys only

from collections import Counter

x = [1, 2, 3, 4, 5]
b = [1, 1, 2, 3, 4, 5]

c = Counter(b) - Counter(x)
keep = list(c) # keys only
print(keep)  # [1]
  • Related