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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:10:56+00:00 2026-05-24T07:10:56+00:00

Using: Python 2.4 Currently, I have a nested for loop that iterates over 2

  • 0

Using: Python 2.4

Currently, I have a nested for loop that iterates over 2 lists and makes a match based on two elements that exists on both lists. Once a match has been found, it the element from the r120Final list and puts in a new list called “r120Delta”:

for r120item in r120Final:
    for spectraItem in spectraFinal:
    if(str(spectraItem[0]) == r120item[2].strip()) and (str(spectraItem[25]) == r120item[10]):
        r120Delta.append(r120item)
        break

The problem is that this is SO SLOW and the lists aren’t that deep. The R120 is about 64,000 lines and the Spectra is about 150,000 lines.

The r120Final list is a nested array and it looks like so:

r120Final[0] = [['xxx','xxx','12345','xxx','xxx','xxx','xxx','xxx','xxx','xxx','234567']]
...
r120Final[n] = [['xxx','xxx','99999','xxx','xxx','xxx','xxx','xxx','xxx','xxx','678901']]

The spectraFinal list is essentially the same, a nested array and it looks like so:

spectraFinal[0] = [['12345','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','234567']]
...
spectraFinal[0] = [['99999','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','xxx','678901']]

Finally, the reason for the “r120Delta” is so that way I can then do a list differential between r120Final and r120Delta and retrieve r120 data elements that were NOT matched. This is the function I defined for this very task, and again, slow:

def listDiff( diffList, completeList ):
    returnList = []
        for completeItem in completeList:
            if not completeItem  in diffList:
                returnList.append(completeItem)
    return returnList

Basically, I’m knowledgeable in Python but by no means an expert. I’m looking for some experts to show me how to speed this up. Any help is appreciated!

  • 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-24T07:10:58+00:00Added an answer on May 24, 2026 at 7:10 am
    spectra_set = set((str(spectraItem[0]), str(spectraItem[25])) for spectraItem in spectraFinal)
    
    returnList = []
    for r120item in r120Final:
        if (r120item[2].strip(), r120item[10]) not in spectra_set:
           returnList.append(r120item)
    

    This will add all items that didn’t match to the returnList.

    You can do it in one line (if you really want) as

    returnList = [r120item for r120item in r120Final 
                     if (r120item[2].strip(), r120item[10]) not in 
                         set((str(spectraItem[0]), str(spectraItem[25])) 
                             for spectraItem in spectraFinal)]
    

    If you need the full spectraItem:

    spectra_dict = dict(((str(spectraItem[0]), str(spectraItem[25])), spectraItem) for spectraItem in spectraFinal)
    returnList = []
    for r120item in r120Final:
        key = (r120item[2].strip(), r120item[10])
        if key not in spectra_dict:
            returnList.append(r120item)
        else:
            return_item = some_function_of(r120item, spectra_dict[key])
            returnList.append(return_item)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have python code that compares two texts using the cosine similarity measure.
I currently have a small Python script that I'm using to spawn multiple executables,
I'm using python-inject , python 2.6 (with unittest2). We have classes to test that
I am currently using Python 2.7. I currently have three columns in an Excel
I currently have an implementation using the pexpect python module, which interacts with Juniper,
I'm currently learning Python using Zelle's Introductory text, and I'm trying to recreate one
How do I authenticate against AD using Python + LDAP. I'm currently using the
Is there a way to determine programmatically, using Python, which web page is currently
Is there a way to determine programmatically, using Python, which web page is currently
I'm currently using the toprettyxml() function of the xml.dom module in a Python script

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.