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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:04:43+00:00 2026-06-02T18:04:43+00:00

I have two lists of strings that are passed into a function. They are

  • 0

I have two lists of strings that are passed into a function. They are more or less the same, except that one has been run through a regex filter to remove certain boilerplate substrings (e.g. removing ‘LLC’ from ‘Blues Brothers LLC’).

This function is meant to internally deduplicate the modified list and remove the associated item in the non-modified list. You can assume that these lists were sorted alphabetically before being run through the regex filter, and remain in the same order (i.e. original[x] and modified[x] refer to the same entity, even if original[x] != modified[x]). Relative order must be maintained between the two lists in the output.

This is what I have so far. It works 99% of the time, except for very rare combinations of inputs and boilerplate strings (1 in 1000s) where some output strings will be mismatched by a single list position. Input lists are ‘original’ and ‘modified’.

# record positions of duplicates so we're not trying to modify the same lists we're iterating
dellist_modified = []
dellist_original = []

# probably not necessary, extra precaution against modifying lists being iterated.
# fwiw the problem still exists if I remove these and change their references in the last two lines directly to the input lists
modified_copy = modified
original_copy = original

for i in range(0, len(modified)-1):
    if modified[i] == modified[i+1]:
dellist_modified.append(modified[i+1])
dellist_original.append(original[i+1])

for j in dellist_modified:
    if j in modified:
        del modified_copy[agg_match.index(j)]
        del original_copy[agg_match.index(j)]

# return modified_copy and original_copy

It’s ugly, but it’s all I got. My testing indicates the problem is created by the last chunk of code.

Modifications or entirely new approaches would be greatly appreciated. My next step is to try using dictionaries.

  • 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-02T18:04:43+00:00Added an answer on June 2, 2026 at 6:04 pm

    Here is a clean way of doing this:

    original = list(range(10))
    modified = list(original)
    modified[5] = "a"
    modified[6] = "a"
    
    def without_repeated(original, modified):
        seen = set()
        for (o, m) in zip(original, modified):
            if m not in seen:
                seen.add(m)
                yield o, m
    
    original, modified = zip(*without_repeated(original, modified))
    
    print(original)
    print(modified)
    

    Giving us:

    (0, 1, 2, 3, 4, 5, 7, 8, 9)
    (0, 1, 2, 3, 4, 'a', 7, 8, 9)
    

    We iterate through both lists at the same time. We keep a set of items we have seen (sets have very fast checks for ownership) and then yields any results that we haven’t already seen.

    We can then use zip again to give us two lists back.

    Note we could actually do this like so:

    seen = set()
    original, modified = zip(*((o, m) for (o, m) in zip(original, modified) if m not in seen and not seen.add(m)))
    

    This works the same way, except using a single generator expression, with adding the item to the set hacked in using the conditional statement (as add always returns false, we can do this). However, this method is considerably harder to read and so I’d advise against it, just an example for the sake of it.

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

Sidebar

Related Questions

I have two lists of strings that, in a better world, would be one
Say I have a trait that has two lists. Sometimes I'm interested in the
I have two lists that I am trying to compare with < or >
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
I have two lists which are guaranteed to be the same length. I want
So I have two comboBoxes (comboBoxFromAccount and comboBoxToAccount). Each has the same datasource, which
Ok so I have two lists in C# List<Attribute> attributes = new List<Attribute>(); List<string>
I have a list like this Dim emailList as new List(Of String) emailList.Add(one@domain.com) emailList.Add(two@domain.com)
I have two lists, I want to sum each element in list A with
I have two lists with values, the expected result is a tuple (a,b) where

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.