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 8057065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:54:41+00:00 2026-06-05T08:54:41+00:00

I have a list of strings (words like), and, while I am parsing a

  • 0

I have a list of strings (words like), and, while I am parsing a text, I need to check if a word belongs to the group of words of my current list.

However, my input is pretty big (about 600 millions lines), and checking if an element belongs to a list is a O(n) operation according to the Python documentation.

My code is something like:

words_in_line = []
for word in line:
    if word in my_list:
        words_in_line.append(word)

As it takes too much time (days actually), I wanted to improve that part which is taking most of the time. I have a look at Python collections, and, more precisely, at deque. However, the only give a O(1) operation time access to the head and the tail of a list, not in the middle.

Do someone has an idea about how to do that in a better way?

  • 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-05T08:54:43+00:00Added an answer on June 5, 2026 at 8:54 am

    You might consider a trie or a DAWG or a database. There are several Python implementations of the same.

    Here is some relative timings for you to consider of a set vs a list:

    import timeit
    import random
    
    with open('/usr/share/dict/words','r') as di:  # UNIX 250k unique word list 
        all_words_set={line.strip() for line in di}
    
    all_words_list=list(all_words_set)    # slightly faster if this list is sorted...      
    
    test_list=[random.choice(all_words_list) for i in range(10000)] 
    test_set=set(test_list)
    
    def set_f():
        count = 0
        for word in test_set:
            if word in all_words_set: 
               count+=1
        return count
    
    def list_f():
        count = 0
        for word in test_list:
            if word in all_words_list: 
               count+=1
        return count
    
    def mix_f():
        # use list for source, set for membership testing
        count = 0
        for word in test_list:
            if word in all_words_set: 
               count+=1
        return count    
    
    print "list:", timeit.Timer(list_f).timeit(1),"secs"
    print "set:", timeit.Timer(set_f).timeit(1),"secs" 
    print "mixed:", timeit.Timer(mix_f).timeit(1),"secs" 
    

    Prints:

    list: 47.4126560688 secs
    set: 0.00277495384216 secs
    mixed: 0.00166988372803 secs
    

    ie, matching a set of 10000 words against a set of 250,000 words is 17,085 X faster than matching a list of same 10000 words in a list of the same 250,000 words. Using a list for the source and a set for membership testing is 28,392 X faster than an unsorted list alone.

    For membership testing, a list is O(n) and sets and dicts are O(1) for lookups.

    Conclusion: Use better data structures for 600 million lines of text!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of strings that contain banned words. What's an efficient way
I have list of strings like this FirstName-Lastname (separated by a dash -) I
I have a list of strings that I need to pass to a process
I have something like this : #tokens is a list of a few words
I have several lists of strings like so, from a possible list of several
I have a huge word list of over 280.000+ words that is loaded from
I have a list of strings that are all early modern English words ending
I have an algorithm that generates strings based on a list of input words.
I have a list of words (assume they are stored in String[] if you
I have a string which is basically a list of words delimited by commas.

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.