array filter in python?

PythonListFilter

Python Problem Overview


For example, I have two lists

 A           = [6, 7, 8, 9, 10, 11, 12]
subset_of_A  = [6, 9, 12]; # the subset of A


the result should be [7, 8, 10, 11]; the remaining elements 

Is there a built-in function in python to do this?

Python Solutions


Solution 1 - Python

If the order is not important, you should use set.difference. However, if you want to retain order, a simple list comprehension is all it takes.

result = [a for a in A if a not in subset_of_A]

EDIT: As delnan says, performance will be substantially improved if subset_of_A is an actual set, since checking for membership in a set is O(1) as compared to O(n) for a list.

A = [6, 7, 8, 9, 10, 11, 12]
subset_of_A = set([6, 9, 12]) # the subset of A

result = [a for a in A if a not in subset_of_A]

Solution 2 - Python

Yes, the filter function:

filter(lambda x: x not in subset_of_A, A)

Solution 3 - Python

set(A)-set(subset_of_A) gives your the intended result set, but it won't retain the original order. The following is order preserving:

[a for a in A if not a in subset_of_A]

Solution 4 - Python

No, there is no build in function in python to do this, because simply:

set(A)- set(subset_of_A)

will provide you the answer.

Solution 5 - Python

tuple(set([6, 7, 8, 9, 10, 11, 12]).difference([6, 9, 12]))

Solution 6 - Python

How about

set(A).difference(subset_of_A)

Solution 7 - Python

This was just asked a couple of days ago (but I cannot find it):

>>> A = [6, 7, 8, 9, 10, 11, 12]
>>> subset_of_A = set([6, 9, 12])
>>> [i for i in A if i not in subset_of_A]
[7, 8, 10, 11]

It might be better to use sets from the beginning, depending on the context. Then you can use set operations like other answers show.

However, converting lists to sets and back only for these operations is slower than list comprehension.

Solution 8 - Python

Use the Set type:

A_set = Set([6,7,8,9,10,11,12])
subset_of_A_set = Set([6,9,12])

result = A_set - subset_of_A_set

Solution 9 - Python

>>> a = set([6, 7, 8, 9, 10, 11, 12])
>>> sub_a = set([6, 9, 12])
>>> a - sub_a
set([8, 10, 11, 7])

Solution 10 - Python

>>> A           = [6, 7, 8, 9, 10, 11, 12]
>>> subset_of_A  = [6, 9, 12];
>>> set(A) - set(subset_of_A)
set([8, 10, 11, 7])
>>> 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkn3lView Question on Stackoverflow
Solution 1 - PythonChinmay KanchiView Answer on Stackoverflow
Solution 2 - PythoncarlpettView Answer on Stackoverflow
Solution 3 - PythonAlexander GesslerView Answer on Stackoverflow
Solution 4 - PythoneatView Answer on Stackoverflow
Solution 5 - PythonNPEView Answer on Stackoverflow
Solution 6 - PythonJoshAdelView Answer on Stackoverflow
Solution 7 - PythonFelix KlingView Answer on Stackoverflow
Solution 8 - PythonPlatinum AzureView Answer on Stackoverflow
Solution 9 - PythonJakeView Answer on Stackoverflow
Solution 10 - PythonAndreas JungView Answer on Stackoverflow