313

If I've got an array of strings, can I check to see if a string is in the array without doing a for loop? Specifically, I'm looking for a way to do it within an if statement, so something like this:

if [check that item is in array]:
user1767754
  • 23,311
  • 18
  • 141
  • 164
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • 3
    I think the question is already answered [here](http://stackoverflow.com/questions/9542738/python-find-in-list) – Tarun Ande Mar 13 '16 at 00:19
  • 1
    Or better here: http://stackoverflow.com/questions/12934190/is-there-a-short-contains-function-for-lists – torina Feb 20 '17 at 22:39

5 Answers5

574

Assuming you mean "list" where you say "array", you can do

if item in my_list:
    # whatever

This works for any collection, not just for lists. For dictionaries, it checks whether the given key is present in the dictionary.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 1
    @jdi, and that loop will run much faster than the one coded explicitly in Python, not to mention being easier to read. – Mark Ransom Jun 28 '12 at 19:44
  • `in` is real nice, but I have to stress how nice using a `lambda` is for cases where we want to check if a substring is in a list of strings. – T.Woody Jul 17 '20 at 21:54
  • @T.Woody [I'd use a generator expression or a list comprehension for that](https://stackoverflow.com/a/4843172/279627), not a lambda. – Sven Marnach Jul 17 '20 at 22:07
  • 1
    And if you need to flip the condition it is `if item not in my_list:` – Mo Zaatar Jun 30 '22 at 03:22
21

I'm also going to assume that you mean "list" when you say "array." Sven Marnach's solution is good. If you are going to be doing repeated checks on the list, then it might be worth converting it to a set or frozenset, which can be faster for each check. Assuming your list of strs is called subjects:

subject_set = frozenset(subjects)
if query in subject_set:
    # whatever
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
8

Use a lambda function.

Let's say you have an array:

nums = [0,1,5]

Check whether 5 is in nums in Python 3.X:

(len(list(filter (lambda x : x == 5, nums))) > 0)

Check whether 5 is in nums in Python 2.7:

(len(filter (lambda x : x == 5, nums)) > 0)

This solution is more robust. You can now check whether any number satisfying a certain condition is in your array nums.

For example, check whether any number that is greater than or equal to 5 exists in nums:

(len(filter (lambda x : x >= 5, nums)) > 0)
T.Woody
  • 1,142
  • 2
  • 11
  • 25
Marquistador
  • 1,841
  • 19
  • 26
  • 3
    This works with Python2. With Python 3.7, you will get this error: `TypeError: object of type 'filter' has no len()` – Jun Sep 20 '19 at 19:59
3

You have to use .values for arrays. for example say you have dataframe which has a column name ie, test['Name'], you can do

if name in test['Name'].values :
   print(name)

for a normal list you dont have to use .values

slfan
  • 8,950
  • 115
  • 65
  • 78
sam komo
  • 39
  • 2
-5

You can also use the same syntax for an array. For example, searching within a Pandas series:

ser = pd.Series(['some', 'strings', 'to', 'query'])

if item in ser.values:
    # do stuff
BCR
  • 960
  • 11
  • 27