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.
arr[~(arr==3).any(1)]Or if you have to usewhere,np.delete(some_list, np.where([3 in sublist for sublist in some_list]), 0)