-1

I want to get unique string from two list. i tried this Get only unique elements from two lists python but it is working for only numbers

a  = ['st','ac','vf']
b = ['st']

output required:

c =['ac','vf']
Smack Alpha
  • 1,828
  • 1
  • 17
  • 37

1 Answers1

0

You can use that as well:

list(set(a).difference(set(b)))

Result:

['ac', 'vf']

EDIT:

In order to make it more robust you can have something like the foloowing:

def diff_lists(l1, l2):
    if len(l1) > len(l2):
        return list(set(l1).difference(set(l2)))
    elif len(l2) > len(l1):
        return list(set(l2).difference(set(l1)))
    else:
        return list(set(l1).difference(set(l2)))

Then it doesn't matter whether you call

diff_lists(a, b)

or

diff_lists(b,a)

The result will still be the same as above.

VnC
  • 1,936
  • 16
  • 26