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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:24:35+00:00 2026-05-28T03:24:35+00:00

I need the following code to finish quicker without threads or multiprocessing. If anyone

  • 0

I need the following code to finish quicker without threads or multiprocessing. If anyone knows of any tricks that would be greatly appreciated. maybe for i in enumerate() or changing the list to a string before calculating, I’m not sure.
For the example below, I have attempted to recreate the variables using a random sequence, however this has rendered some of the conditions inside the loop useless … which is ok for this example, it just means the ‘true’ application for the code will take slightly longer.
Currently on my i7, the example below (which will mostly bypass some of its conditions) completes in 1 second, I would like to get this down as much as possible.

import random
import time
import collections
import cProfile


def random_string(length=7):
    """Return a random string of given length"""
    return "".join([chr(random.randint(65, 90)) for i in range(length)])

LIST_LEN = 18400
original = [[random_string() for i in range(LIST_LEN)] for j in range(6)]
LIST_LEN = 5
SufxList = [random_string() for i in range(LIST_LEN)]
LIST_LEN = 28
TerminateHook = [random_string() for i in range(LIST_LEN)]
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Exclude above from benchmark


ListVar = original[:]
for b in range(len(ListVar)):
   for c in range(len(ListVar[b])):

       #If its an int ... remove
       try:
           int(ListVar[b][c].replace(' ', ''))
           ListVar[b][c] = ''
       except: pass

       #if any second sufxList delete
       for d in range(len(SufxList)):
           if ListVar[b][c].find(SufxList[d]) != -1: ListVar[b][c] = ''

       for d in range(len(TerminateHook)):
           if ListVar[b][c].find(TerminateHook[d]) != -1: ListVar[b][c] = ''
   #remove all '' from list
   while '' in ListVar[b]: ListVar[b].remove('')

print(ListVar[b])
  • 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-05-28T03:24:36+00:00Added an answer on May 28, 2026 at 3:24 am
    ListVar = original[:]
    

    That makes a shallow copy of ListVar, so your changes to the second level lists are going to affect the original also. Are you sure that is what you want? Much better would be to build the new modified list from scratch.

    for b in range(len(ListVar)):
       for c in range(len(ListVar[b])):
    

    Yuck: whenever possible iterate directly over lists.

           #If its an int ... remove
           try:
               int(ListVar[b][c].replace(' ', ''))
               ListVar[b][c] = ''
           except: pass
    

    You want to ignore spaces in the middle of numbers? That doesn’t sound right. If the numbers can be negative you may want to use the try..except but if they are only positive just use .isdigit().

           #if any second sufxList delete
           for d in range(len(SufxList)):
               if ListVar[b][c].find(SufxList[d]) != -1: ListVar[b][c] = ''
    

    Is that just bad naming? SufxList implies you are looking for suffixes, if so just use .endswith() (and note that you can pass a tuple in to avoid the loop). If you really do want to find the the suffix is anywhere in the string use the in operator.

           for d in range(len(TerminateHook)):
               if ListVar[b][c].find(TerminateHook[d]) != -1: ListVar[b][c] = ''
    

    Again use the in operator. Also any() is useful here.

       #remove all '' from list
       while '' in ListVar[b]: ListVar[b].remove('')
    

    and that while is O(n^2) i.e. it will be slow. You could use a list comprehension instead to strip out the blanks, but better just to build clean lists to begin with.

    print(ListVar[b])
    

    I think maybe your indentation was wrong on that print.

    Putting these suggestions together gives something like:

    suffixes = tuple(SufxList)
    newListVar = []
    for row in original:
       newRow = []
       newListVar.append(newRow)
       for value in row:
           if (not value.isdigit() and 
               not value.endswith(suffixes) and
               not any(th in value for th in TerminateHook)):
               newRow.append(value)
    
        print(newRow)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that I need to add an additonal object to
The following code works, but I'm curious as to why I need the Path
The following code, prints out Derived Base Base But I need every Derived object
I need syntax help with the following code logic: I have a code block
i have the following code ..i need to loop through end of the file
Working with the following code, I need to return only records where the `point'
I need to calculate the time complexity of the following code: for (i =
Please your opinion on the following code. I need to calculate the diff in
The xmlns attribute in the following code stops me getting the value I need.
I've got the following code, and need to strip all non alpha numeric characters.

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.