625

How can I check if any of the strings in an array exists in another string?

For example:

a = ['a', 'b', 'c']
s = "a123"
if a in s:
    print("some of the strings found in s")
else:
    print("no strings found in s")

How can I replace the if a in s: line to get the appropriate result?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
jahmax
  • 8,181
  • 7
  • 26
  • 25
  • 9
    I'm surprised there aren't (yet) any answers comparing to a compiled regex in terms of perf, especially compared to size of the string and number of "needles" to search for. – Pat Apr 22 '15 at 23:21
  • 6
    @Pat I am not surprised. The question is not about performance. Today most programmers care more for getting it done and readability. The performance question is valid, but a different question. – guettli Jul 13 '16 at 06:42
  • regex `[abc]` also works perfectly well and will be faster if there are more than a couple of candidates to test. But if the strings are arbitrary and you don't know them in advance to construct a regex, you will have to use the `any(x in str for x in a)` approach. – smci Jan 08 '20 at 13:15
  • This problem is a special case of https://stackoverflow.com/questions/1342601/. The standard approach is to use `any`, as seen in the top answers; however, some string-specific optimizations may be possible. – Karl Knechtel Aug 02 '22 at 23:42
  • Well I did look up this question for performance. Otherwise that’s a bachelor-grade algorithmic problem. – Maëlan Aug 19 '23 at 21:46

19 Answers19

1225

You can use any:

a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]

if any([x in a_string for x in matches]):

Similarly to check if all the strings from the list are found, use all instead of any.

Steve Jalim
  • 11,989
  • 1
  • 37
  • 54
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 17
    any() takes an iterable. I am not sure which version of Python you are using but in 2.6 you will need to put [] around your argument to any(). any([x in str for x in a]) so that the comprehension returns an iterable. But maybe later versions of Python already do this. – emispowder Mar 27 '13 at 01:06
  • 8
    @Mark Byers: Sorry for the late comment, but is there a way to print the string that was found? How would you do this. Thank you. – Shankar Kumar Aug 01 '13 at 01:26
  • 4
    Not sure I understand, if a is the list, and str is the thing to match against, what is the x? Python newbie ftw. :) – red Nov 13 '13 at 14:01
  • 2
    @red: you can read `for x in a` like "for each element in list". Since `a` is a list of strings, and `x` is an element of that list, `x` is a string (one of 'a', 'b', 'c' in original example) – User Jan 27 '14 at 20:50
  • 7
    @emispowder It works fine for me as-is in Python 2.6.9. – MPlanchard Jul 10 '15 at 18:25
  • 7
    @emispowder: [Generator expressions](https://www.python.org/dev/peps/pep-0289/) were introduced in 2.4. – zondo Apr 22 '17 at 03:07
  • What is x representing in this ans – CodeGuru Jun 11 '18 at 00:17
  • One caveat of using 'any' is that it verifies if each string of 'a' is in 'str'. You might want to return True as soon as one string of 'a' is in 'str'. This would be faster. – huseyin39 Feb 06 '20 at 22:03
  • 1
    @CodeGuru `x` is just a temporary variable when iterating through `a`. You could use a different name like `[number*2 for number in [7, 13]]`, but `[x*2 for x in [7, 13]]` seems to be a kind of convention. – Qaswed Feb 26 '20 at 14:15
  • 2
    Basically [x in str for x in a] returns a list of True/False depending on whether the string is found. The any() function iterates true that list of True/False and returns True if there is one True in the list. @CodeGuru – chia yongkang Apr 28 '20 at 03:07
  • 1
    I cleaned up the code to use `a_string` instead of Python reserved word `str` and `matches` instead of `a` because the reserved word was needlessly confusing (I know the poster had it, but still) and nested list comprehensions are impenetrable with single letter variables. – rjurney Apr 29 '20 at 00:35
  • I can't figure out a way to match a capitalized word. a_string = "A string is MORE than its parts!" matches = ["MORE", "wholesome", "milk"] This does not match 'MORE'. What's the issue here? – Vipul Priyadarshi Apr 02 '22 at 07:46
  • For a Python newbie, would any senior please expand `if any(x in a_string for x in matches):` to explicit loop? This seems not correct: `for x in a_string: print(x) if x in matches: print(f'{x=} is in the matches')` – MathArt Oct 13 '22 at 08:59
  • @MathArt: generator expressions and list comprehensions are a little backwards from what you expect. The expression at the beginning (`x in a_string`) would be the final line if you expanded to a `for` loop. So `for x in matches: print(x in a_string)` will print a bunch of either True or False. If any one of those is True, the `any()` function will return True as well. – zondo Oct 22 '22 at 13:14
  • I think this is suboptimal because `x in a_string for x in matches` will evaluate all strings, output a list of booleans, then `any()` will loop over the list and break when `True` is found. Using a plain `for` loop allows you to break the loop as soon as the first element is found, avoiding unnecessary evaluations of the other strings. – Aurélien Pierre Oct 23 '22 at 13:22
112

any() is by far the best approach if all you want is True or False, but if you want to know specifically which string/strings match, you can use a couple things.

If you want the first match (with False as a default):

match = next((x for x in a if x in a_string), False)

If you want to get all matches (including duplicates):

matches = [x for x in a if x in a_string]

If you want to get all non-duplicate matches (disregarding order):

matches = {x for x in a if x in a_string}

If you want to get all non-duplicate matches in the right order:

matches = []
for x in a:
    if x in a_string and x not in matches:
        matches.append(x)
mirekphd
  • 4,799
  • 3
  • 38
  • 59
zondo
  • 19,901
  • 8
  • 44
  • 83
  • please add example for the last match too – Oleg Kokorin Apr 02 '18 at 21:46
  • @OlegKokorin: It creates a list of matching strings in the same order it finds them, but it keeps only the first one if two are the same. – zondo Apr 04 '18 at 00:35
  • Using an `OrderedDict` is probably more performant than a list. See [this answer on "Removing duplicates in lists"](https://stackoverflow.com/a/7961390/4518341) – wjandrea May 18 '20 at 00:11
  • Can you provide an example? – Herwini Nov 16 '20 at 14:18
  • 1
    One nice thing about the first option (using next) is it allows for short-circuit evaluation, so if you just want to know, for example, when a failure happened, if it was any of a certain class of failures, you don't have to generate truth values for each comparison, just all of them until the first match is found. – hlongmore Apr 05 '23 at 04:29
62

You should be careful if the strings in a or str gets longer. The straightforward solutions take O(S*(A^2)), where S is the length of str and A is the sum of the lenghts of all strings in a. For a faster solution, look at Aho-Corasick algorithm for string matching, which runs in linear time O(S+A).

jbernadas
  • 2,540
  • 18
  • 12
  • can Aho-Corasick also find substrings instead of prefixes ? – RetroCode Sep 26 '16 at 19:58
  • 3
    Some python Aho-Corasick libraries are [here](https://pypi.python.org/pypi/pyahocorasick/) and [here](https://github.com/JanFan/py-aho-corasick) – vorpal Sep 27 '17 at 10:54
41

Just to add some diversity with regex:

import re

if any(re.findall(r'a|b|c', str, re.IGNORECASE)):
    print 'possible matches thanks to regex'
else:
    print 'no matches'

or if your list is too long - any(re.findall(r'|'.join(a), str, re.IGNORECASE))

Shankar ARUL
  • 12,642
  • 11
  • 68
  • 69
  • 1
    This works for the given use case of the question. If the you search for `(` or `*` this fails, since quoting for the regex syntax needs to be done. – guettli Jul 12 '16 at 10:13
  • 5
    You can escape it if necessary with `'|'.join(map(re.escape, strings_to_match))`. You sould probably `re.compile('|'.join(...))` as well. – Artyer Nov 04 '17 at 21:50
  • 3
    And What's the time complexity ? – DachuanZhao Apr 30 '21 at 01:51
18

A surprisingly fast approach is to use set:

a = ['a', 'b', 'c']
a_string = "a123"
if set(a) & set(a_string):
    print("some of the strings found in a_string")
else:
    print("no strings found in a_string")

This works if a does not contain any multiple-character values (in which case use any as listed above). If so, it's simpler to specify a as a string: a = 'abc'.

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Berislav Lopac
  • 16,656
  • 6
  • 71
  • 80
14

You need to iterate on the elements of a.

a = ['a', 'b', 'c']
a_string = "a123"
found_a_string = False
for item in a:    
    if item in a_string:
        found_a_string = True

if found_a_string:
    print "found a match"
else:
    print "no match found"
mirekphd
  • 4,799
  • 3
  • 38
  • 59
Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
  • 3
    Yes i knew how to do that but compared to Marks answer, that's horrible code. – jahmax Aug 02 '10 at 16:24
  • 14
    Only if you understand Mark's code. The problem you were having is that you weren't examining the elements of your array. There are a lot of terse, pythonic ways to accomplish what you want that would hide the essence of what was wrong with your code. – Seamus Campbell Aug 02 '10 at 16:38
  • 14
    It may be 'horrible code' but it's [exactly what any() does](http://docs.python.org/2/library/functions.html#any). Also, this gives you the actual string that matched, whereas any() just tells you there is a match. – alldayremix Apr 01 '13 at 15:21
4
a = ['a', 'b', 'c']
str =  "a123"

a_match = [True for match in a if match in str]

if True in a_match:
  print "some of the strings found in str"
else:
  print "no strings found in str"
mluebke
  • 8,588
  • 7
  • 35
  • 31
4

jbernadas already mentioned the Aho-Corasick-Algorithm in order to reduce complexity.

Here is one way to use it in Python:

  1. Download aho_corasick.py from here

  2. Put it in the same directory as your main Python file and name it aho_corasick.py

  3. Try the alrorithm with the following code:

    from aho_corasick import aho_corasick #(string, keywords)
    
    print(aho_corasick(string, ["keyword1", "keyword2"]))
    

Note that the search is case-sensitive

Domi W
  • 574
  • 10
  • 15
3

The regex module recommended in python docs, supports this

words = {'he', 'or', 'low'}
p = regex.compile(r"\L<name>", name=words)
m = p.findall('helloworld')
print(m)

output:

['he', 'low', 'or']

Some details on implementation: link

balki
  • 26,394
  • 30
  • 105
  • 151
3

A compact way to find multiple strings in another list of strings is to use set.intersection. This executes much faster than list comprehension in large sets or lists.

>>> astring = ['abc','def','ghi','jkl','mno']
>>> bstring = ['def', 'jkl']
>>> a_set = set(astring)  # convert list to set
>>> b_set = set(bstring)
>>> matches = a_set.intersection(b_set)
>>> matches
{'def', 'jkl'}
>>> list(matches) # if you want a list instead of a set
['def', 'jkl']
>>>
  • The input is specified as a list of strings vs. a longer string that might contain them as a substring, not as two lists of strings. – Karl Knechtel Aug 02 '22 at 23:44
2

Just some more info on how to get all list elements availlable in String

a = ['a', 'b', 'c']
str = "a123" 
list(filter(lambda x:  x in str, a))
Nilesh Birari
  • 873
  • 7
  • 13
2

I needed to do that in a performance-critical environment, so I benchmarked all the possible variants I could find and think of with Python 3.11. Here are the results:

words =['test', 'èk', 'user_me', '<markup>', '[^1]']

def find_words(words):
    for word in words:
        if "_" in word or "<" in word or ">" in word or "^" in word:
            pass

def find_words_2(words):
    for word in words:
        for elem in [">", "<", "_", "^"]:
            if elem in word:
                pass

def find_words_3(words):
    for word in words:
        if re.search(r"\_|\<|\>|\^", word):
            pass

def find_words_4(words):
    for word in words:
        if re.match(r"\S*(\_|\<|\>|\^)\S*", word):
            pass

def find_words_5(words):
    for word in words:
        if any(elem in word for elem in [">", "<", "_", "^"]):
            pass

def find_words_6(words):
    for word in words:
        if any(map(word.__contains__, [">", "<", "_", "^"])):
            pass
> %timeit find_words(words)
351 ns ± 6.24 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

> %timeit find_words_2(words)
689 ns ± 15.4 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

> %timeit find_words_3(words)
2.42 µs ± 43.9 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

> %timeit find_words_4(words)
2.75 µs ± 146 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

> %timeit find_words_5(words)
2.65 µs ± 176 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

> %timeit find_words_6(words)
1.64 µs ± 28.6 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
  1. The naive chained or approach wins (function 1)
  2. The basic iteration over each element to test (function 2) is at least 50% faster than using any(), and even a regex search is faster than the basic any() without map(), so I don't get why it exists at all. Not to mention, the syntax is purely algorithmic so any programmer will understand what it does, even without Python background.
  3. re.match() only searches for patterns starting at the beginning of the line (which is confusing if you come from PHP/Perl regex), so to make it work like PHP/Perl, you need to use re.search() or to tweak the regex to include characters before, which comes with a performance penalty.

If the list of substrings to search for is known at programming time, the ugly chained or is definitely the way to go. Otherwise, use the basic for loop over the list of substrings to search. any() and regex are a loss of time in this context.

For a more down-to-earth application (searching if a file is an image by looking for its extension in a list):

def is_image(word: str ) -> bool:
  if  ".bmp" in word or \
      ".jpg" in word or \
      ".jpeg" in word or \
      ".jpe" in word or \
      ".jp2" in word or \
      ".j2c" in word or \
      ".j2k" in word or \
      ".jpc" in word or \
      ".jpf" in word or \
      ".jpx" in word or \
      ".png" in word or \
      ".ico" in word or \
      ".svg" in word or \
      ".webp" in word or \
      ".heif" in word or \
      ".heic" in word or \
      ".tif" in word or \
      ".tiff" in word or \
      ".hdr" in word or \
      ".exr" in word or \
      ".ppm" in word or \
      ".pfm" in word or \
      ".nef" in word or \
      ".rw2" in word or \
      ".cr2" in word or \
      ".cr3" in word or \
      ".crw" in word or \
      ".dng" in word or \
      ".raf" in word or \
      ".arw" in word or \
      ".srf" in word or \
      ".sr2" in word or \
      ".iiq" in word or \
      ".3fr" in word or \
      ".dcr" in word or \
      ".ari" in word or \
      ".pef" in word or \
      ".x3f" in word or \
      ".erf" in word or \
      ".raw" in word or \
      ".rwz" in word:
    return True
  return False

IMAGE_PATTERN = re.compile(r"\.(bmp|jpg|jpeg|jpe|jp2|j2c|j2k|jpc|jpf|jpx|png|ico|svg|webp|heif|heic|tif|tiff|hdr|exr|ppm|pfm|nef|rw2|cr2|cr3|crw|dng|raf|arw|srf|sr2|iiq|3fr|dcr|ari|pef|x3f|erf|raw|rwz)")

extensions = [".bmp", ".jpg", ".jpeg", ".jpe", ".jp2", ".j2c", ".j2k", ".jpc", ".jpf", ".jpx", ".png", ".ico", ".svg", ".webp", ".heif", ".heic", ".tif", ".tiff", ".hdr", ".exr", ".ppm", ".pfm", ".nef", ".rw2", ".cr2", ".cr3", ".crw", ".dng", ".raf", ".arw", ".srf", ".sr2", ".iiq", ".3fr", ".dcr", ".ari", ".pef", ".x3f", ".erf", ".raw", ".rwz"]

(Note that the extensions are declared in the same order in all variants).

> %timeit is_image("DSC_blablabla_001256.nef") # found
536 ns ± 18.3 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

> %timeit is_image("DSC_blablabla_001256.noop") # not found
923 ns ± 43.8 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

> %timeit IMAGE_PATTERN.search("DSC_blablabla_001256.nef")
221 ns ± 24.3 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

> %timeit IMAGE_PATTERN.search("DSC_blablabla_001256.noop") # not found
207 ns ± 4.3 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

> %timeit any(ext in "DSC_blablabla_001256.nef" for ext in extensions) # found
1.53 µs ± 30.1 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

> %timeit any(ext in "DSC_blablabla_001256.noop" for ext in extensions) # not found
2.2 µs ± 25.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

With a lot more options to test, regex are actually faster and more legible (for once…) than the chained or. any() ist still the worst.

Empiric tests show that the performance threshold is at 9 elements to test:

  • below 9 elements, chained or is faster,
  • above 9 elements, regex search() is faster,
  • at exactly 9 elements, both run around 225 ns.
Aurélien Pierre
  • 604
  • 1
  • 6
  • 20
1

It depends on the context suppose if you want to check single literal like(any single word a,e,w,..etc) in is enough

original_word ="hackerearcth"
for 'h' in original_word:
      print("YES")

if you want to check any of the character among the original_word: make use of

if any(your_required in yourinput for your_required in original_word ):

if you want all the input you want in that original_word,make use of all simple

original_word = ['h', 'a', 'c', 'k', 'e', 'r', 'e', 'a', 'r', 't', 'h']
yourinput = str(input()).lower()
if all(requested_word in yourinput for requested_word in original_word):
    print("yes")
Trinadh Koya
  • 1,089
  • 15
  • 19
  • What would be yourinput? I can recognise two things: the sentence where I'm looking for something. The array of words I'm looking for. But you describe three variables and I can't get what the third one is. – mayid May 19 '19 at 22:43
1
flog = open('test.txt', 'r')
flogLines = flog.readlines()
strlist = ['SUCCESS', 'Done','SUCCESSFUL']
res = False
for line in flogLines:
     for fstr in strlist:
         if line.find(fstr) != -1:
            print('found') 
            res = True


if res:
    print('res true')
else: 
    print('res false')

output example image

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
LeftSpace
  • 84
  • 1
  • 7
1

I would use this kind of function for speed:

def check_string(string, substring_list):
    for substring in substring_list:
        if substring in string:
            return True
    return False
1

Yet another solution with set. using set.intersection. For a one-liner.

subset = {"some" ,"words"} 
text = "some words to be searched here"
if len(subset & set(text.split())) == len(subset):
   print("All values present in text")

if subset & set(text.split()):
   print("Atleast one values present in text")
sjd
  • 1,329
  • 4
  • 28
  • 48
1

If you want exact matches of words then consider word tokenizing the target string. I use the recommended word_tokenize from nltk:

from nltk.tokenize import word_tokenize

Here is the tokenized string from the accepted answer:

a_string = "A string is more than its parts!"
tokens = word_tokenize(a_string)
tokens
Out[46]: ['A', 'string', 'is', 'more', 'than', 'its', 'parts', '!']

The accepted answer gets modified as follows:

matches_1 = ["more", "wholesome", "milk"]
[x in tokens for x in matches_1]
Out[42]: [True, False, False]

As in the accepted answer, the word "more" is still matched. If "mo" becomes a match string, however, the accepted answer still finds a match. That is a behavior I did not want.

matches_2 = ["mo", "wholesome", "milk"]
[x in a_string for x in matches_1]
Out[43]: [True, False, False]

Using word tokenization, "mo" is no longer matched:

[x in tokens for x in matches_2]
Out[44]: [False, False, False]

That is the additional behavior that I wanted. This answer also responds to the duplicate question here.

0
data = "firstName and favoriteFood"
mandatory_fields = ['firstName', 'lastName', 'age']


# for each
for field in mandatory_fields:
    if field not in data:
        print("Error, missing req field {0}".format(field));

# still fine, multiple if statements
if ('firstName' not in data or 
    'lastName' not in data or
    'age' not in data):
    print("Error, missing a req field");

# not very readable, list comprehension
missing_fields = [x for x in mandatory_fields if x not in data]
if (len(missing_fields)>0):
    print("Error, missing fields {0}".format(", ".join(missing_fields)));
Robert I
  • 1,509
  • 2
  • 11
  • 18
0

I found this question from a link from another closed question: Python: How to check a string for substrings from a list? but don't see an explicit solution to that question in the above answers.

Given a list of substrings and a list of strings, return a unique list of strings that have any of the substrings.

substrings = ['hello','world','python']
strings = ['blah blah.hello_everyone','this is a-crazy_world.here',
       'one more string','ok, one more string with hello world python']
# one-liner
list(set([strings_of_interest for strings_of_interest in strings for substring in substrings if substring in strings_of_interest]))
cwa
  • 131
  • 2