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

  • Home
  • SEARCH
  • 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 8777081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:12:29+00:00 2026-06-13T19:12:29+00:00

I want to maintain a list of strings and keep checking for different variables

  • 0

I want to maintain a list of strings and keep checking for different variables if they are in the list.

What I’m doing is :

li=[one,two,three]
for x in different_x_values:
  if(x in li):
    print 'yes'

Is there a faster way than using x in li that checks if a some string is in some other collection of strings?

  • 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-13T19:12:31+00:00Added an answer on June 13, 2026 at 7:12 pm

    I am sure you already got your answer, this is for the sake of completeness. Out of curiosity I ran the tests for the methods mentioned I got following results.

    Size=size of main list
    setopttime: the one that uses s1&s2
    setintersectiontime: the one using intersection operation
    bruteforcetime:burte force time

    Size=1000
    setoptime:0:00:00.001000
    setintersectiontime:0:00:00.001000
    bruteforcetime:0:00:00.005000

    Size=10000
    setoptime:0:00:00.001000
    setintersectiontime:0:00:00.010000
    bruteforcetime:0:00:00.367000

    Size=100000
    setoptime:0:00:00.001000
    setintersectiontime:0:00:00.115000
    bruteforcetime:0:00:35.444000

    Method 1 (setopttime) wins hands down. Here is the code if you want to check.

    import timeit
    import datetime
    def getList(size):
        s=[]
        for i in range(size):
            s.append(str(i))
        return s
    
    def getSubList(size):
        s=[]
        for i in range(size/2):
            s.append(str(i))
        return s
    
    def testSet(size):
        s=set(getList(size))
        sublist=set(getSubList(size))
        list(s&sublist)
    
    
    def testIntersection(size):
        s=set(getList(size))
        sublist=set(getSubList(size))
        list(s.intersection(sublist))
    
    def bruteForce(size):
        s=getList(size)
        sublist=getSubList(size)
        final=[]
        for elem in sublist:
            try:
                if s.index(elem)!=-1:
                    final.append(elem)
            except ValueError:
                pass
    currentsize=1000
    for i in range(3):
        t1=datetime.datetime.now()
        testSet(1000)
        t2=datetime.datetime.now()
        testIntersection(currentsize)
        t3=datetime.datetime.now()
        bruteForce(currentsize)
        t4=datetime.datetime.now()
        print "Size="+str(currentsize)
        print "setoptime:"+str(t2-t1)
        print "setintersectiontime:"+str(t3-t2)
        print "bruteforcetime:"+str(t4-t3)
        currentsize=currentsize*10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have one Drupal copy to maintain, but a lot of sites
I have a list of lists of three strings in Python, e.g., m =
Lets say I want to maintain a list of items per user (in MongoDB
I want to have a window's menu item maintain a list of open windows
If I want to maintain an ordered list in Firebase, it seems like the
Hello I have a small List<string> that I want to maintain across postbacks on
I have a page in which I want to maintain the value of object
I want to create and maintain a relatively simple relational database in my java
I want my window to always maintain a certain ratio of let's say 1.33333333.
I want zip up a folder structured like so: temp/folder1/file1 temp/folder2/file2 temp/file3 and maintain

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.