list comprehsions

List comprehension in Python is a concise and readable way to create lists. It allows you to generate a new list by applying an expression to each item in an existing iterable (like a list, tuple, or range), optionally including a condition.

chevron-rightExamplehashtag

Basic Syntax:

[expression for item in iterable]

With a condition:

[expression for item in iterable if condition]

Examples:

  1. Without condition:

pythonCopyEditsquares = [x**2 for x in range(5)]
# Output: [0, 1, 4, 9, 16]
  1. With condition (even numbers only):

pythonCopyEditevens = [x for x in range(10) if x % 2 == 0]
# Output: [0, 2, 4, 6, 8]
  1. Using string operations:

pythonCopyEditwords = ["hello", "world", "python"]
uppercased = [word.upper() for word in words]
# Output: ['HELLO', 'WORLD', 'PYTHON']

List comprehension is often preferred over traditional for loops for creating lists because it's shorter and more expressive. However, it's best to avoid overcomplicating them—readability should always come first.

chevron-rightlist comprehension use cases for DevOps/SRE engineershashtag

Sure, Pavan! Here's an extended version of the list comprehension use cases for DevOps/SRE engineers — with code + detailed explanations for each case.


✅ 1. Filtering Running Docker Containers

import subprocess

containers = subprocess.getoutput("docker ps --format '{{.Names}}'").splitlines()
running_nginx = [c for c in containers if 'nginx' in c]

🔍 Explanation:

  • docker ps lists all running containers.

  • We use .splitlines() to convert the output to a list.

  • The list comprehension filters out containers with names containing "nginx".

Real use: Used in automation scripts to act only on specific containers (e.g., restart or collect logs).


✅ 2. Generate Prometheus Targets

ips = ["192.168.1.10", "192.168.1.11", "192.168.1.12"]
targets = [f'"{ip}:9100"' for ip in ips]

🔍 Explanation:

  • You have a list of IPs for your node_exporter targets.

  • This creates properly formatted strings for Prometheus config.

Real use: Useful when dynamically creating prometheus.yml using Python.


✅ 3. Extract Error Lines from Logs

logs = [
    "INFO: service started",
    "ERROR: DB connection failed",
    "ERROR: Timeout",
    "INFO: job completed"
]
errors = [line for line in logs if "ERROR" in line]

🔍 Explanation:

  • Iterates through each log line.

  • Selects only those that contain the word "ERROR".

Real use: Helps quickly parse logs for debugging or alerting.


✅ 4. Find Unhealthy Kubernetes Pods

pods = [
    {"name": "api-pod", "status": "Running"},
    {"name": "db-pod", "status": "CrashLoopBackOff"},
    {"name": "worker-pod", "status": "Pending"}
]

unhealthy = [pod["name"] for pod in pods if pod["status"] != "Running"]

🔍 Explanation:

  • You may fetch this pod info using kubectl or the Kubernetes Python client.

  • This code filters out the pods that are not running.

Real use: Automated health checks or custom dashboards.


✅ 5. Find Large Files in a Directory

import os

files = os.listdir('/var/log')
large_logs = [f for f in files if os.path.getsize(f"/var/log/{f}") > 10 * 1024 * 1024]

🔍 Explanation:

  • Lists all files in /var/log.

  • Checks file sizes and returns only those greater than 10MB.

Real use: Disk cleanup automation or alerting on oversized log files.


✅ 6. Detect High-Usage Metrics for Alerting

metrics = {
    "cpu_usage": 85,
    "memory_usage": 70,
    "disk_usage": 92
}

alerts = [k for k, v in metrics.items() if v > 80]

🔍 Explanation:

  • metrics dictionary holds current resource usage.

  • This code picks the metric names where usage exceeds 80%.

Real use: Use this before sending alerts (e.g., via Slack, Email, or PagerDuty).


✅ 7. Convert EC2 Instance IDs from API

instances = [
    {"InstanceId": "i-abc123", "State": "running"},
    {"InstanceId": "i-def456", "State": "stopped"},
    {"InstanceId": "i-ghi789", "State": "running"}
]

running_ids = [i["InstanceId"] for i in instances if i["State"] == "running"]

🔍 Explanation:

  • Filters only running EC2 instances from API response.

  • Returns their InstanceId.

Real use: Target only active instances for updates or deployment.


💡 Summary:

Use Case
Purpose

Docker container filtering

Act on specific containers

Prometheus config generation

Dynamic targets for monitoring

Log filtering

Extract errors for alerts

Pod health check

Identify non-running pods

Disk cleanup

Find large files in log directories

Custom metric alerting

Trigger actions on threshold breach

AWS EC2 filtering

Automate targeting of live instances


Let me know if you want a GitHub repo or Python script collection for these. Would make a great DevOps utility toolbox!

chevron-rightuses c for c, line for line, pod for for, f for fhashtag

Great observation, Pavan! 👍

The variable names like c, line, pod, f in list comprehensions are not fixed — you can use any valid variable name you like.


✅ Example: All of these are valid:

squares = [x**2 for x in range(5)]          # common convention
squares = [num**2 for num in range(5)]      # more descriptive
squares = [value**2 for value in range(5)]  # completely fine

They all produce:

[0, 1, 4, 9, 16]

✅ Same for dictionaries, logs, etc.:

errors = [line for line in logs if "ERROR" in line]  # descriptive
errors = [log_entry for log_entry in logs if "ERROR" in log_entry]  # also fine

🔁 Why use short names like c, f, or x?

  • For very short and simple expressions.

  • When the context is obvious (like f in a file loop).

  • It keeps the code concise and readable.

But for more complex logic, descriptive names are preferred:

[instance["InstanceId"] for instance in instances if instance["State"] == "running"]

💡 Tip:

Use:

  • Short names for quick one-liners or simple logic.

  • Descriptive names when clarity matters, especially in team/shared code.

Let me know if you'd like a naming best practices guide for Python in DevOps scripts.

Last updated