Python for Network Automation - Part 5: Diving Deeper with Data Structures and Regex


In our journey through Python for network automation, we've touched on foundational concepts and their applications. This fifth installment dives deeper, exploring Python's core data structures and the magic of regular expressions. These foundational concepts are pivotal for efficient data manipulation and pattern matching, especially in the realm of network automation. Let's get started!


Dictionaries in Python are versatile data structures that store key-value pairs. They offer a structured way to represent data, making them invaluable for tasks like device configurations or network parameters.

Key Concepts

Creation: Dictionaries can be created using curly braces {} or the dict() constructor.
Access: Values can be accessed using their corresponding keys, e.g., dict_name[key].
Modification: Values can be updated by assigning new data to a key.
Handling: Non-existent Keys: Methods like get() can be used to avoid KeyError.
Mutability: Dictionaries can be altered after creation, making them flexible for data storage.

Creating a dictionary

# Creating a dictionary
device = {
    'ip_addr': '',
    'vendor': 'cisco',
    'model': '3850',
    'uptime': '2 days'

# Access & Modification
print(device['vendor'])  # Output: cisco
device['uptime'] = '3 days'

# Handling Non-existent Keys
print(device.get('location', 'N/A'))  # Output: N/A

Dictionary Methods

Python provides a plethora of methods to enhance our interactions with dictionaries, making data manipulation a breeze.

Key Methods:

`get()`: Fetches the value for a given key, and returns a default if the key is absent.
`copy()`: Returns a shallow copy of the dictionary.
`pop()`: Removes a key-value pair.
`update()`: Merges two dictionaries.
Iteration: Loops can be used to traverse keys, values, or key-value pairs, providing a way to iterate through dictionary data.         

Python provides methods to interact with dictionaries:

# Using the get() method
os_version = device.get('os_version', 'Unknown')
print(os_version)  # Output: Unknown

# Updating the dictionary
device.update({'os_version': 'IOS-XE 16.12.3'})
print(device['os_version'])  # Output: IOS-XE 16.12.3


Sets in Python are collections of unique elements. They are particularly useful when dealing with non-duplicated items and offer powerful set operations.
Key Concepts:

  - Uniqueness: Each element in a set is distinct.
  - Unordered: Sets do not maintain element order.
  - Operations: Union, intersection, difference, and symmetric difference.

Sets store unique elements, making them ideal for handling non-duplicated items.

# Creating sets
houston_ips = {'', '', ''}
atlanta_ips = {'', ''}

# Set operations
common_ips = houston_ips.intersection(atlanta_ips)
mon_ips)  # Output: {''}


Exceptions in Python handle unexpected events during code execution, allowing for more robust and error-resistant programs.
Key Concepts:

  - Try/Except: Capture and respond to errors.
  - Raising Exceptions: Use `raise` to trigger an exception.
  - Multiple Exceptions: Handle different error types with multiple `except` blocks.
  - Finally: Execute code regardless of an exception occurrence.

except KeyError:
    print("The key 'location' does not exist.") 

Regular Expressions

Regular expressions (regex) are powerful tools for pattern matching and data extraction. They are indispensable for tasks like data validation, searching, and text manipulation.
Key Concepts:
Special Characters: Symbols like `.`, `+`, `*`, and `^` have special meanings in regex.
  - Character Classes: Represent a set of characters, e.g., `\d` for digits.
  - Groups: Use parentheses `()` to capture specific parts of a match.
  - Flags: Modify the regex behavior, e.g., `re.I` for case-insensitive matching.

import re
text = "The IP address is"
match ='(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', text)
if match:
    ip_address =
    print(ip_address)  # Output:


1. Dictionaries: Create a dictionary representing a network device. Add, modify, and retrieve key-value pairs. Explore methods like `update()` and `get()`.
2. Sets: Create sets for IP addresses from different data centers. Use set operations to find common and unique IPs.
3. Exceptions: Write a script that handles errors gracefully using try/except blocks.
4. Regular Expression: Extract data from given text using regex patterns. Explore methods like ``, `re.findall()`, and `re.sub()`.

import re
# Dictionary creation and manipulation
device = {
    'ip_addr': '',
    'vendor': 'cisco',
    'model': '3850',
    'uptime': '2 days'
device['location'] = 'Houston'
device.update({'os_version': 'IOS-XE 16.12.3'})

# Set operations
houston_ips = {'', '', ''}
atlanta_ips = {'', ''}
common_ips = houston_ips.intersection(atlanta_ips)

# Exception handling
    os_version = device['os_version']
except KeyError:
    os_version = 'Unknown'

# Regular expression matching
text = f"The device {device['model']} at {device['ip_addr']} runs {os_version}."
match ='(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', text)
if match:
    ip_address =

print(f"Device IP: {ip_address}, Common IPs: {common_ips}")

Use Case: Streamlining VLAN Configuration

Picture a bustling scenario where a network engineer is tasked with configuring VLANs across a myriad of switches in a sprawling network. Undertaking this manually is not only time-consuming but also a fertile ground for errors. However, with Python as a tool, this task can be automated, ensuring pinpoint accuracy and saving a substantial amount of time.

import re
# A dictionary to hold our network devices and their configurations
network_devices = {
    'Switch1': {
        'ip': '',
        'config': 'vlan 10\nname Sales\nvlan 20\nname Engineering\n'
    'Switch2': {
        'ip': '',
        'config': 'vlan 10\nname Sales\nvlan 30\nname HR\n'
    # ... more devices

def extract_vlans(config):
    """Harnessing the power of regex to extract VLAN information."""
    vlan_info = re.findall(r'vlan (\d+)\nname (\w+)', config)
    return {int(vlan): name for vlan, name in vlan_info}

def configure_device(device_ip, vlans):
    """A simulated device configuration showcasing VLAN info."""
    print(f'Configuring device {device_ip}')
    for vlan, name in vlans.items():
        print(f'Creating VLAN {vlan} with name {name}')

# A journey through each device, extracting VLAN info, and configuring the device
for device, info in network_devices.items():
    config = info['config']
    device_ip = info['ip']
    vlans = extract_vlans(config)
    configure_device(device_ip, vlans)


1. Data Structure (Dictionary):
   - The `network_devices` dictionary is a structured representation of our network, where each device is a key, and the value is another dictionary encapsulating the IP address and configuration data of the device.
   - The `extract_vlans` function crafts a dictionary where each key is a VLAN ID and each value is the corresponding VLAN name, showcasing the flexibility and utility of dictionaries in data representation and manipulation.

2. Regular Expressions:
   - The `re.findall` method, coupled with a regex pattern in the `extract_vlans` function, meticulously extracts VLAN information from the configuration data. The pattern `r'vlan (\d+)\nname (\w+)'` is tailored to match and capture the VLAN ID and name, demonstrating the prowess of regex in data extraction and pattern matching.

3. Automation:
   - The `configure_device` function, although a simulation, paints a picture of how a real-world device configuration could look like. In a real-world scenario, this function could be powered by libraries like Netmiko or Napalm to connect to the device and apply the configurations, showcasing Python's capability in network automation.
   - The loop iterates through each device in the `network_devices` dictionary, extracts the VLAN information using the `extract_vlans` function, and configures the device using the `configure_device` function, painting a vivid picture of automation in action.

This use case is a testament to how Python, armed with its core data structures and regular expressions, can be a formidable tool in the hands of network engineers, streamlining the process of network configurations, and making the network more efficient and error-resistant. The journey through Python's capabilities in network automation is not only compelling but also opens a vista of possibilities, making the life of network engineers less cumbersome and more productive.


This installment has equipped you with deeper insights into Python's data structures and the art of regex. As you continue your journey in network automation, these tools will prove invaluable, enabling you to handle data more efficiently and match patterns with precision. Stay tuned for more insights in our upcoming blogs. Happy coding!