Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists
#2
Don't modify list while iterating over it.

How to remove items from a list while iterating?


In your case you can construct working_config directly with list comprehension, while iterating over config. i.e. no need to create copy and then remove elements from it
working_config = [item for item in config if "GigabitEthernet" in item]
Note, there is one more problem - because row is a tuple (due to using enumerate) where first element is row index, and the second element - string resulting from splitting lines in the file, I would expect no elements will be removed.

Could you provide sample config file in full (i.e. not full content but how it looks unmodified)
Finally, you may want to have on third-party package like https://pypi.org/project/ciscoconfparse/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Lists - by blake7 - Oct-06-2023, 11:22 AM
RE: Lists - by buran - Oct-06-2023, 11:37 AM
RE: Lists - by blake7 - Oct-06-2023, 11:54 AM
RE: Lists - by blake7 - Oct-06-2023, 12:08 PM
RE: Lists - by buran - Oct-06-2023, 12:09 PM
RE: Lists - by buran - Oct-06-2023, 12:29 PM
RE: Lists - by buran - Oct-06-2023, 12:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 3,293 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 5,028 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020