1

I have a list some_list = [[1, 2], [3, 4], [3, 6]] I want to find some index where expression was evaluated as true:

np.where([3 in sublist for sublist in some_list])

The output is (array([1, 2], dtype=int64),).

Since I'd like to remove sublist with 3 inside, how can I access such array (in an elegant way)? With the array I can do [some_list.pop(index) for index in array].

Edit: seems like it works using for index in np.where([3 in sublist for sublist in some_list])[0]

2
  • 2
    [a for a in some_list if not 3 in a] Commented Jun 27, 2020 at 22:37
  • 1
    Or arr[~(arr==3).any(1)] Or if you have to use where, np.delete(some_list, np.where([3 in sublist for sublist in some_list]), 0) Commented Jun 27, 2020 at 22:40

1 Answer 1

2

where just returns a tuple of arrays that index where the element values True.

In [447]: some_list = [[1, 2], [3, 4], [3, 6]]                                  

Your list test:

In [448]: [3 in sublist for sublist in some_list]                               
Out[448]: [False, True, True]
In [449]: np.where([3 in sublist for sublist in some_list])                     
Out[449]: (array([1, 2]),)

That's a one element tuple, for a 1 dimensional list [448]. We can extract that array with simple indexing:

In [450]: _[0]                                                                  
Out[450]: array([1, 2])

and use it to select sublists from some_list:

In [451]: [some_list[i] for i in _]                                             
Out[451]: [[3, 4], [3, 6]]

If the list was an array:

In [455]: arr = np.array(some_list)                                             
In [456]: arr                                                                   
Out[456]: 
array([[1, 2],
       [3, 4],
       [3, 6]])

we could do the same search for 3 with:

In [457]: arr==3                                                                
Out[457]: 
array([[False, False],
       [ True, False],
       [ True, False]])
In [458]: (arr==3).any(axis=1)                                                  
Out[458]: array([False,  True,  True])
In [459]: np.where(_)                                                           
Out[459]: (array([1, 2]),)

That [459] tuple can be used to index the [458] array directly. In this case it can also be used to index rows of arr:

In [460]: arr[_]                                                                
Out[460]: 
array([[3, 4],
       [3, 6]])

Here that tuple derived from 1d [458] works, but if it didn't we could (again) extract the array with indexing, and use that:

In [461]: np.where((arr==3).any(axis=1))[0]                                     
Out[461]: array([1, 2])
In [462]: arr[_, :]                                                             
Out[462]: 
array([[3, 4],
       [3, 6]])

===

A pure-list way of doing this:

In [476]: [i for i,sublist in enumerate(some_list) if 3 in sublist]                     
Out[476]: [1, 2]

It could well be faster, since np.where converts list inputs to arrays, and that takes time.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I love the pure list! Thanks a lot for the time you spent. I had figured the first one but the full list is just pure beauty.

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.