Skip to content
Advertisement

Replace just a string’s portion in a list from a dictionary

I am struggling when trying to replace string’s portions from a list, using a dictionary. I have a list with network IPs and subnet masks:

JavaScript

My idea is to create a dictionary to replace the subnet masks into prefix masks, for example:

JavaScript

I am getting these IPs after using a small code to get IPs from a huge document, and my current code looks like this:

JavaScript

Advertisement

Answer

Try something like this:

JavaScript

This will also replace your subnet masks with empty strings if a corresponding prefix mask is not present in the dictionary.

Also dict is a keyword in python dont use it as variable names.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement