Chapter 18: Writing Smarter Network Scripts with Python Lambdas

Python lambdas help you write composable, testable, and elegant logic for device filtering, config validation, and more.

1. Why Lambdas Matter in Network Automation

In network engineering, clarity and efficiency are not just desirable. They’re essential. We routinely deal with thousands of lines of configuration, complex inventories of devices, and logic trees that branch based on vendor quirks, interface status, or routing policy. To manage this growing complexity, we automate our processes. And when we automate, we inevitably write logic; logic that transforms, filters, and validates data before we take action.

This is where lambdas come in.

At first glance, a lambda function in Python might seem like a syntactic novelty: a one-liner function without a name, useful only for simple cases. But when you’re building scripts to iterate over route tables, filter interface lists, or sort BGP neighbors by uptime, lambdas become a sharp, elegant tool in your automation toolkit.

Imagine you’ve pulled telemetry from 500 switches, and you want to identify the interfaces that are both up and have a speed mismatch. The traditional approach might be to write a loop, check each interface, build a result list, and return it. That works, but it’s verbose, and when you start repeating that same filtering logic across different scripts, the overhead becomes real. Lambdas allow you to express intent inline, filtering or transforming network data with minimal ceremony. They shine in those micro-tasks: pass a condition to a filter, provide a custom key for sorting, or transform values on-the-fly, all without defining a separate function.

In other words, lambdas reduce friction.

Think about the sorted() function. If you’ve ever needed to sort a list of devices by their BGP prefix count or CPU usage, you likely passed a custom function to the key parameter. That function can be a full def block, but often, all you’re doing is returning device["prefixes"]. A lambda does this in one clean line, keeping your focus on the bigger picture.

More than just saving keystrokes, lambdas also allow you to write more expressive code. When paired with Python’s functional programming features like map(), filter(), and reduce(), you can turn multi-step operations into tight, declarative logic. This is particularly powerful when you’re dealing with network inventory data, policy rules, or telemetry streams, where data must be filtered and transformed before it can be acted upon.

Let’s not forget another key angle: readability. Yes, lambdas can be abused and become cryptic when overused, but in well-scoped applications, they make your logic flow naturally. “Filter all down interfaces.” “Sort these peers by local preference.” “Map all interface speeds to Gbps.” These are business-readable operations. And lambdas make them code-readable, too.

Lastly, in modern network engineering toolchains, especially when working with libraries like Nornir or Netmiko, or even parsing structured data from REST APIs, you will frequently encounter scenarios where passing small functions as parameters is necessary. Knowing how to leverage lambdas in these moments is a sign of fluency, and a way to write smarter, more idiomatic automation.

In short, lambdas aren’t about being fancy. They’re about precision and economy. And in network automation, where we’re often transforming structured data to generate config, validate state, or drive decisions, lambdas provide just enough abstraction to keep the code elegant while staying readable and maintainable.

Let’s demystify them entirely and understand what a lambda function really is and what it isn’t.

Subscribe to keep reading

This content is free, but you must be subscribed to The Routing Intent by Leonardo Furtado to continue reading.

Already a subscriber?Sign in.Not now