-1

Let's say I have two lists per account id (table below). I want to remove any elements that appear in list 1 (competitor_old) that also appear in list 2 (competitor_new). Essential L2 - L1 per row.

So from the table below I would have a new column (competitor_new_unique) and for the first row the a value would be c,d

table

      account_id competitor_old competitor_new 
          234241            a,b        a,b,c,d
           53266              h            h,s
          342536              j              j
          325632              s          s,g,e
          324346              f              f

desired result

 account_id competitor_old competitor_new competitor_new_unique
      234241            a,b        a,b,c,d                   c,d
       53266              h            h,s                     s
      342536              j              j
      325632              s          s,g,e                   g,e
      324346              f              f

Most of my efforts can be derived from another post. Remove all the elements that occur in one list from another

Thanks for any input.

J_Mohan
  • 1
  • 1
  • Are they lists or sets? I mean, are the values unique within the lists? Also, does order matter? – Riccardo Bucco Dec 28 '19 at 12:53
  • 2
    Input is in which form of data structure? How is the table stored? – Saurav Sahu Dec 28 '19 at 12:55
  • "Most of my efforts can be derived from another post..." Can you show us your latest attempt? – jpp Dec 28 '19 at 12:57
  • It seems you are looking for `set` operations. Try something like `set(['a', 'b', 'c', 'd']) - set(['a', 'b'])`. – accdias Dec 28 '19 at 13:08
  • @RiccardoBucco - The values are unique within the lists. The order does not matter. I am getting my table from a CSV file I have tried using set but I get the resulting erreor TypeError: unhashable type: 'list' As for my attempts `so = data.competitor_old sn = data.competitor_new s1 = [elem.strip().split(',') for elem in so] s2 = [elem.strip().split(',') for elem in sn]` Attempts `sf = [x for x in sn1 if x not in so1]` `lambda x: x not in s1, s2` `set(sn1) - set(so1)` – J_Mohan Dec 28 '19 at 15:07

1 Answers1

0
for id in account_id:
    id["competitor_new_unique"] = [x for x in id["competitor_new"] if x not in id["competitor_old"]] 
JasonLauHK
  • 41
  • 4