简体   繁体   中英

Merge two lists in to one list and discard the duplicates. Pandas Python

Want to merge two lists and discard the intersecting elements

A = ['a', 'b', 'c', 'd']

B = ['a', 'b', 'd', 'e', 'f']

Expected result:

['c', 'e', 'f']

I can get this by:

[i for i in A if i not in B] + [i for i in B if i not in A]

But is there a more convenient way to get the same result without loops and preferably through Pandas.

Best regards

Use sets:

set(A).symmetric_difference(B)

or equivalent:

set(A)^set(B)

(You can convert back to list if needs to be...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM