0
word_line_dict={'abc': ['av', '202', '4140'], 'rcd': ['j00001', '42000', 'j400', '1mm', '440', '1adcmm', '1cmm'], 'lac': ['46', '62', 'm111cmm', '485', 'm111', 't0ta1', 'cmm'], 'halo': ['175', '1355', 'gnt10', '1380', 'gmb10'], 'asd': ['440', '1adcmm', '225000', '1cmm', 'c0unt']}

for keys in word_line_dict:
  for val in word_line_dict[keys]:
    if val.isnumeric() != True:
      word_line_dict[keys].remove(val)

print(word_line_dict)

the output of this code is:

{'abc': ['202', '4140'], 'rcd': ['42000', '1mm', '440', '1cmm'], 'lac': ['46', '62', '485', 't0ta1'], 'halo': ['175', '1355', '1380'], 'asd': ['440', '225000', 'c0unt']}

i just cannot get why this code is not removing the words like 1mm,1cmm,t0tal,c0unt but running this same code again on the output list removes all these words, my question is why is it not doing it in the first one only?

what i tried:

for keys in word_line_dict:
  temp_list=word_line_dict[keys]
    for val in temp_list:
      if not val.isnumeric():
        word_line_dict[keys].remove(val)
Aakash Kaushik
  • 130
  • 1
  • 9
  • FWIW, `!= True` should basically never be used, use `if not val.isnumeric()`. – deceze May 23 '20 at 09:21
  • @deceze i took a look at the question where it was specified that editing a list while traversing is not a good idea, but i created a duplicate list and still the thing follow, i also edited the question to what i tried. – Aakash Kaushik May 23 '20 at 09:52
  • It’s still the same list, just assigned to a different name. You didn’t copy the list at all. – deceze May 23 '20 at 10:06
  • @deceze thanks a lot, that worked and got to know about lists a bit more. – Aakash Kaushik May 23 '20 at 10:12

0 Answers0