1

I'm running a code to clean a database. Basically, if some value appears in a list they should be removed.

Below you can see the code:

    pattern = re.compile("((?:\d{10}|\d{9}|\d{8}|\d{7}|\d{6}|\d{5}|\d{4})(?:-?[\d]))?(?!\S)")
    cc = pattern.findall(a)
    print("cpf:", cpf)
    print("ag:", ag)
    print("cc start:",cc)
    for i in cc:
        print("i:",i)
        try:
            while i in ag: cc.remove(i)
        except:pass
        try:
            while i in cpf:cc.remove(i)
        except:pass
        try:
            while "" in i:cc.remove(i)
        except:pass
    print("final cc:",cc)

It prints in my screen the following:

cpf: ['00770991092']
ag: 3527
cc start: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '00770991092', '', '', '', '', '', '', '', '', '01068651-0', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
i: 
i: 01068651-0
final cc: ['00770991092']

Well, the '' values are removed, that's seem to be working fine. However since '00770991092' is a value inside cpf it should've been removed, but it hasn't. In the "final cc" that's the value I'm getting and it should be '01068651-0'.

Even If I run this check: if cc in cpf:print(True) It confirms it is True.

What am I missing?

PS.: I find quite intriguing that when I print(i) inside the for sentence only two values show up (and one is empty).

aabujamra
  • 4,494
  • 13
  • 51
  • 101
  • 2
    You probably shouldn’t alter the list you are looping over. – Mark Apr 14 '20 at 01:39
  • Using except like this is a bad idea, see https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except. – AMC Apr 14 '20 at 01:42

1 Answers1

1

Modifying a list as you're iterating over it doesn't work very well. Is building a new list an option? Something like:

filtered_cc = [
    i for i in cc
    if not (i in ag or i in cpf or i == "")
]
Samwise
  • 68,105
  • 3
  • 30
  • 44