Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an element exists in a Python array (Equivalent of PHP in_array)?

I'm new to Python and I'm looking for a standard function that would tell me if an element is present in an array. I found the index method but it throws an exception if the element is not found. I just need some simple function that would return true if the element is in the array or false if not.

Basically an equivalent to PHP in_array.

like image 666
laurent Avatar asked Feb 07 '13 03:02

laurent


People also ask

How do you check if an element is in an array Python?

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.

How do you check if a value exists in an array in PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.

What does the In_array () function do?

The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

How do you check if a value exists in an array?

The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.


1 Answers

>>> 1 in [0, 1, 2, 3, 4, 5]
True
like image 125
Ignacio Vazquez-Abrams Avatar answered Oct 24 '22 04:10

Ignacio Vazquez-Abrams