6

I have a list of tuples like this:

listOfTuples = [(0, 1), (0, 2), (3, 1)]

and an array that could look like this:

myArray = np.array([-2, 9, 5])

Furthermore, I have an array with Boolean expressions which I created like this:

dummyArray = np.array([0, 1, 0.6])
myBooleanArray =  dummyArray < 1

myBooleanArray therefore looks like this:

array([True, False, True], dtype=bool)

Now I would like to extract values from listOfTuples and myArray based on myBooleanArray. For myArray it is straight forward and I can just use:

myArray[myBooleanArray]

which gives me the desired output

[-2  5]

However, when I use

listOfTuples[myBooleanArray]

I receive

TypeError: only integer arrays with one element can be converted to an index

A workaround would be to convert this list to an array first by doing:

np.array(listOfTuples)[myBooleanArray]

which yields

[[0 1]
 [3 1]]

Is there any smarter way of doing this? My desired output would be

[(0, 1), (3, 1)]
0

2 Answers 2

7

Python list object, unlike Numpy array, doesn't support boolean indexing directly. For that you could use itertools.compress function:

>>> from itertools import compress
>>> list(compress(listOfTuples,bool_array))
[(0, 1), (3, 1)]

Note that one of the advantages of compress along side its functional structure which can be very useful in many cases, is that it returns a generator and its very memory efficient in cases where you have a very large list object to filter.

If you want you can also to loop over the result if you wish to process the items one by one instead of converting the whole object to a list:

for item in compress(listOfTuples,bool_array):
     #do stuff
Sign up to request clarification or add additional context in comments.

1 Comment

That works great! And thanks a lot for pointing out the generator part, very helpful since I will indeed work on huge lists! I'll upvote for now and accept it later on as well.
2

The answer by Kasra is the best this is just an alternate

In [30]: [i[0] for i in list(zip(listOfTuples,bools)) if i[1] == True ]
Out[30]: [(0, 1), (3, 1)]

1 Comment

Thanks for submitting also an alternative even if it is less efficient than Kasra's; it works fine! I upvote it as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.