Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:17:29+00:00 2026-06-17T12:17:29+00:00

What is the fastest way to check if a string contains some characters from

  • 0

What is the fastest way to check if a string contains some characters from any items of a list?

Currently, I’m using this method:

lestring = "Text123"

lelist = ["Text", "foo", "bar"]

for x in lelist:
    if lestring.count(x):
        print 'Yep. "%s" contains characters from "%s" item.' % (lestring, x)

Is there any way to do it without iteration (which will make it faster I suppose.)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:17:30+00:00Added an answer on June 17, 2026 at 12:17 pm

    You can try list comprehension with membership check

    >>> lestring = "Text123"
    >>> lelist = ["Text", "foo", "bar"]
    >>> [e for e in lelist if e in lestring]
    ['Text']
    

    Compared to your implementation, though LC has an implicit loop but its faster as there is no explicit function call as in your case with count

    Compared to Joe’s implementation, yours is way faster, as the filter function would require to call two functions in a loop, lambda and count

    >>> def joe(lelist, lestring):
        return ''.join(random.sample(x + 'b'*len(x), len(x)))
    
    >>> def uz(lelist, lestring):
        for x in lelist:
            if lestring.count(x):
                return 'Yep. "%s" contains characters from "%s" item.' % (lestring, x)
    
    
    >>> def ab(lelist, lestring):
        return [e for e in lelist if e in lestring]
    
    >>> t_ab = timeit.Timer("ab(lelist, lestring)", setup="from __main__ import lelist, lestring, ab")
    >>> t_uz = timeit.Timer("uz(lelist, lestring)", setup="from __main__ import lelist, lestring, uz")
    >>> t_joe = timeit.Timer("joe(lelist, lestring)", setup="from __main__ import lelist, lestring, joe")
    >>> t_ab.timeit(100000)
    0.09391469893125759
    >>> t_uz.timeit(100000)
    0.1528471407273173
    >>> t_joe.timeit(100000)
    1.4272649857800843
    

    Jamie’s commented solution is slower for shorter string’s. Here is the test result

    >>> def jamie(lelist, lestring):
        return next(itertools.chain((e for e in lelist if e in lestring), (None,))) is not None
    
    >>> t_jamie = timeit.Timer("jamie(lelist, lestring)", setup="from __main__ import lelist, lestring, jamie")
    >>> t_jamie.timeit(100000)
    0.22237164127909637
    

    If you need Boolean values, for shorter strings, just modify the above LC expression

    [e in lestring for e in lelist if e in lestring]
    

    Or for longer strings, you can do the following

    >>> next(e in lestring for e in lelist if e in lestring)
    True
    

    or

    >>> any(e in lestring for e in lelist)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the fastest way to check that a String contains only alphanumeric characters.
What is the fastest way to check if a file contains a certain string
What's the fastest way using either DOS scripting or PowerShell to run this simple
Possible Duplicate: Convert string to int C++ What’s the fastest way to check if
What is the simplest and fastest way to check if string is single URL
What is the fastest way to check if Hbase table exists? Looking at this
What is the fastest way to check if a string matches a certain pattern?
Which way is really the fastest way to check for an empty string and
I see that over on this question LINQy way to check if any objects
I am trying to find the fastest way to check whether a given number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.