Search code examples
pythonpandaslist-comparison

Checking if an element exist in list


I have 2 lists(pandas.core.series.Series) and I want to list elements that doesn't exist in the other series. So I'm using 'not in' operator but it doesn't work and the code prints the whole list instead.

for i in list1:
  if i not in list2:
    print(i)

This code prints the whole list1 instead of printing elements that don't exist in list2. I know this should be a simple task but I'm stuck, can someone help me?


Solution

  • You can just use sets and calculate the set difference.

    set(list1).difference(set(list2))