-4

I have a list ('a') in python that is my original list. I have a second list ('b'). I want to compare list 'a' and 'b' and create a new list ('c') that I want to take action on. List 'c' should only contain unique values from b that are not in a. (See examples below.)

Original list: a = ['apple', 'orange', 'pear']

New list has a duplicate entry from 'a' ('pear'): b = ['pear', 'banana', 'grape']

Desired final output list with new items only: c = ['banana', 'grape']

Lastly, when I'm done, I want to update my original list so that it contains everything: ['apple', 'orange', 'pear','banana', 'grape']

nia4life
  • 333
  • 1
  • 5
  • 17

1 Answers1

0

This may do the needful:

c = [each for each in b if each not in a ]

To update the original list, a simple extend would do like:

 a.extend(c)
rkatkam
  • 2,634
  • 3
  • 18
  • 29
  • how would I do the final part of my request?: Lastly, when I'm done, I want to update my original list so that it contains everything: ['apple', 'orange', 'pear','banana', 'grape'] – nia4life May 11 '16 at 20:24
  • Updated the post. You may try now. – rkatkam May 12 '16 at 04:58