Home > Back-end >  How can I remove the same values from arrays and put the remnants in two different arrays
How can I remove the same values from arrays and put the remnants in two different arrays

Time:05-03

I have two arrays with string values, they have similar values and different ones, how can I remove the same values from them and put the remnants in two different arrays? Example from below

one_ar = ['Python', 'Java', 'C']
two_ar = ['Python', 'Lua']

one_result = ['Java', 'C']
two_result = ['Lua']

CodePudding user response:

A simple solution would be

one_result = [x in one_ar if x not in two_ar]
two_result = [x in two_ar if x not in one_ar]

If you need it with efficient scaling to large size, you might prefer something else.

CodePudding user response:

You can do set arithmetic on the arrays as sets. first create a set from the list. then do the intersection of the arrays and save that result. then from each of the original arrays subtract the intersection array. the result should be each of the original arrays should now contain their unique values that were not shared.

  • Related