0

I want to substract an array from another one which contains it. For example I have two arrays: array1 and array 2, where array2 is contained in array1.

array1 = ["a", "b", "c", "d", "e"]
array2 = ["a", "b"]

And I want to do array1 - array2 which would be stored in a third array: array3

array3 = ["c", "d", "e"]

Thanks in advance for any help provided

  • Possible duplicate of [Get difference between two lists](http://stackoverflow.com/questions/3462143/get-difference-between-two-lists) – ahmed Apr 10 '16 at 23:04

2 Answers2

1

You can use sets for this set(array1) - set(array2)

Or, if you really wanted to do a list comprehension

array_3 = [x for x in array_1 if x not in array_2]
Pythonista
  • 11,377
  • 2
  • 31
  • 50
0

You can use set for that:

>>> array1 = ["a", "b", "c", "d", "e"]
>>> array2 = ["a", "b"]

>>> list(set(array1) - set(array2))
['c', 'e', 'd']
ahmed
  • 5,430
  • 1
  • 20
  • 36