0

I am using Python 2.7

I have two CSV files with similar lists, but with a few differences. I can compare the two files to get a list of strings of the records from the input list that do not match the correct list. But when iterating over the list of unmatched strings, I run into an issue...

I keep a simple count of the records that I process through in my for loop. When I get to the else statement, I remove spaces and when I remove the unmatched item from the list, I run into my issue. My noMatchList has 30 records. When that remove happens, I only get a count of 20 iterations instead of a full 30. With that remove commented out, I get a full 30 iterations.

Any idea what's going on here? Thanks for your time.

My relevant code:

for unMatched in noMatchList:
    # Check if an uppercased version of the unMatched item is in GISlist. If so, remove the unMatched item
    count +=1
    upperCased = unMatched.upper()
    if upperCased in GISlist:
        noMatchList.remove(unMatched)
    else:
        # Check if the uppercased version of the unmatched item with no spaces around hyphen is in GISlist
        # If it is in the GISlist, remove it. 
        if ' - ' in upperCased:
            noSpaces = upperCased[0:upperCased.index('-')-1] + '-' + upperCased[upperCased.index('-')+2:]
            if noSpaces in GISlist:
                noMatchList.remove(unMatched)
A.Thom
  • 27
  • 1
  • 5
  • 1
    Except pass is an [antipattern that you should avoid](https://realpython.com/blog/python/the-most-diabolical-python-antipattern/). – marcusshep Sep 06 '17 at 14:29
  • Possible duplicate of [Remove items from a list while iterating](https://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating) – SiHa Sep 06 '17 at 14:55
  • I used the try-except to try and account for when the unmatched record i'm iterating over does not have a ' - ' in it. But I have changed my code above to take that out. – A.Thom Sep 06 '17 at 15:08

1 Answers1

1

Found what I was looking for in this thread

When starting my for loop, i put a slice onto the noMatchList and it worked like a charm! Example:

for unMatched in noMatchList[:]:
A.Thom
  • 27
  • 1
  • 5
  • But do you know *why* it worked? This is a fairly fundamental Python thing, and it would pay you to understand it. – SiHa Sep 07 '17 at 07:12
  • I believe so. By adding the slice, it takes all of the items in the list when i start the loop. Rather than without the slice, where it looks at all the items in a list that is constantly changing. – A.Thom Sep 07 '17 at 15:56