AppDividend
Latest Code Tutorials

Python Array Contains: Check If Array Contains Elements

0

Python does not have built-in support for Arrays, but you can create or use the arrays using the numpy library.

Python Array Contains

To check if an array contains an element or not in Python, use the in operator. The in operator checks whether a specified element is an integral element of a sequence like string, array, list, tuple, etc.

To work with the numpy library, you need to install numpy in your Python environment. Then import the library using the import statement.

import numpy as np

To create an array in Python, use the np.array() method.

arr = np.array([11, 19, 21])

To check if the array contains a “19” element, use the in operator.

import numpy as np

arr = np.array([11, 19, 21])

element_exist = 19 in arr

print(element_exist)

Output

True

That means the array contains the “19” element, and it is True because it is there in the array.

Let me demonstrate an example where the array does not contain the provided element.

import numpy as np

arr = np.array([11, 19, 21])

element_exist = 18 in arr

print(element_exist)

Output

False

That is it. This is how you can verify if the element exists in the array or not in Python.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.