The Python Oracle

How can i check that a list is in my array in python

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay

--

Chapters
00:00 How Can I Check That A List Is In My Array In Python
00:26 Answer 1 Score 0
01:00 Answer 2 Score 3
01:13 Accepted Answer Score 6
03:18 Answer 4 Score 1
03:33 Thank you

--

Full question
https://stackoverflow.com/questions/5304...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x

#avk47



ACCEPTED ANSWER

Score 6


Try this generator comprehension. The builtin any() short-circuits so that you don't have extra evaluations that you don't need.

any(np.array_equal(row, B) for row in A)

For now, np.array_equal doesn't implement internal short-circuiting. In a different question the performance impact of different ways of accomplishing this is discussed.

As @Dan mentions below, broadcasting is another valid way to solve this problem, and it's often (though not always) a better way. For some rough heuristics, here's how you might want to choose between the two approaches. As with any other micro-optimization, benchmark your results.

Generator Comprehension

  • Reduced memory footprint (not creating the array B==A)
  • Short-circuiting (if the first row of A is B, we don't have to look at the rest)
  • When rows are large (definition depends on your system, but could be ~100 - 100,000), broadcasting isn't noticeably faster.
  • Uses builtin language features. You have numpy installed anyway, but I'm partial to using the core language when there isn't a reason to do otherwise.

Broadcasting

  • Fastest way to solve an extremely broad range of problems using numpy. Using it here is good practice.
  • If we do have to search through every row in A (i.e. if more often than not we expect B to not be in A), broadcasting will almost always be faster (not always a lot faster necessarily, see next point)
  • When rows are smallish, the generator expression won't be able to vectorize the computations efficiently, so broadcasting will be substantially faster (unless of course you have enough rows that short-circuiting outweighs that concern).
  • In a broader context where you have more numpy code, the use of broadcasting here can help to have more consistent patterns in your code base. Coworkers and future you will appreciate not having a mix of coding styles and patterns.



ANSWER 2

Score 3


You can do it by using broadcasting like this:

import numpy as np
A = np.array([[2,3,4],[5,6,7]])
B = np.array([2,3,4]) # Or [2,3,4], a list will work fine here too

(B==A).all(axis=1).any()



ANSWER 3

Score 1


Using the built-in any. As soon as an identical element is found, it stops iterating and returns true.

import numpy as np

A = np.array([[2,3,4],[5,6,7]])
B = [3,2,4]

if any(np.array_equal(B, x) for x in A):
  print(f'{B} inside {A}')
else:
  print(f'{B} NOT inside {A}')



ANSWER 4

Score 0


You need to use .all() for comparing all the elements of list.

A = np.array([[2,3,4],[5,6,7]])
B = [2,3,4]

for i in A:
    if (i==B).all():
        print ("Yes, B is present in A")
        break

EDIT: I put break to break out of the loop as soon as the first occurence is found. This applies to example such as A = np.array([[2,3,4],[2,3,4]])

# print ("Yes, B is present in A")

Alternative solution using any:

any((i==B).all() for i in A)

# True