array([[10, 11, 12, 13, 26, 28, 11],
       [ 4,  9, 19, 20, 21, 26, 12],
       [ 1,  7, 10, 23, 28, 32, 16],
       [ 4,  6,  7, 10, 13, 25,  3],
       [ 4,  6, 15, 17, 30, 31, 16]])

test = np.array([10, 11, 12, 13, 26, 28, 11])


def check(test,array):
    for i in array:
        if np.array_equal(i,test):
    print('not in')

Is there any simple method without using for...in...if...else structure?

  • \$\begingroup\$ I thought that, but then I checked it and it doesn't work for numpy arrays. \$\endgroup\$
    – Josiah
    Commented May 7, 2018 at 12:56
  • \$\begingroup\$ i, obviously, didn't take my pills this morning. sorry \$\endgroup\$
    – bobrobbob
    Commented May 7, 2018 at 15:13

2 Answers 2


There are indeed ways. The most pythonic one is probably to use a generator comprehension.

def check(test,array):
    return any(numpy.array_equal(x, test) for x in array)

Note that this is not quite the same behaviour as it returns a Boolean rather than printing the result, but that behaviour should be easy enough to add outside.

As an aside, I don't like the variable name test. It sounds to me like the name of a predicate function rather than the name of an array you want to look for.

The name check is also rather too general to usefully say what it is checking.

Now I'm perhaps overly fond of slightly too descriptive names, but I'd probably be calling this def is_row_in_array(row, array):

  • \$\begingroup\$ What about any(map(lambda x:np.array_equal(x,test), array)) ? Is it more pythonic? \$\endgroup\$
    – Jack
    Commented May 7, 2018 at 14:12
  • \$\begingroup\$ That's controversial. As near as I can tell the trend is slightly to prefer comprehensions, and in particular to prefer comprehensions when you would otherwise have to define a lambda. Performance concerens also prefer comprehensions to map with a lambda. Of course some people, including myself, do find map more natural to think in. I actually wrote the map version in this answer before translating it to a generator comprehension! \$\endgroup\$
    – Josiah
    Commented May 7, 2018 at 14:38
  • \$\begingroup\$ For a bit of history about just how controversial, Guido van Rossum, the original creator of Python tried to drop map, filter and lambda from Python 3! artima.com/weblogs/viewpost.jsp?thread=98196 \$\endgroup\$
    – Josiah
    Commented May 7, 2018 at 14:57

If you don't like the looping and the if ... statements you could use solely NumPy methods:

def is_row_in_array(row , arr):
    return np.any(np.sum(np.abs(arr-row), axis=1) == 0)
  • \$\begingroup\$ (arr == row).all(axis=1).any() is shorter and a bit faster, but the general approach in this answer is right. \$\endgroup\$ Commented May 8, 2018 at 13:46
  • \$\begingroup\$ @Gareth Rees, nice improvement. \$\endgroup\$
    – Jan Kuiken
    Commented May 8, 2018 at 15:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.