0

I have written code to remove items in a list that contains a substring that matches another list. In other words, to remove items from cols if the item contains a substring from exclude_cols

exclude_cols =map(str.lower, exclude_cols)
exclude_cols.remove('')
for x in exclude_cols:
    for c in cols:
        if x in c:
            #print "dropping ", x, c
            cols.remove(c)

This partially works because I have to run it 3 times, to remove all the items needed. It is not iterating as I expect. Is there any other way I can accomplish the intent without running the same code multiple times?

Cody
  • 389
  • 3
  • 4
  • 11
  • Can you give an example of input, show the output it creates, and the output you expect? – glibdud Jan 11 '18 at 17:24
  • If `exclude_cols` is a list of `str`. `for c in cols:` iterate over each character. – Zulu Jan 11 '18 at 17:25
  • If `exclude_cols` has value `cpr_wlan_efuse_targ_volt` and `cols` has value `m_520000276_cpr_wlan_efuse_targ_volt_nom_tdo_tdo`, I expect the first output to be `dropping cpr_wlan_efuse_targ_volt m_520000276_cpr_wlan_efuse_targ_volt_nom_tdo_tdo` but that always only outputs in the second run. – Cody Jan 11 '18 at 17:29
  • Absent a full example (in the question itself), I'm inclined to agree that it's related to modifying a list while you're iterating over it. – glibdud Jan 11 '18 at 17:38

0 Answers0