You want to pull just the ERROR lines out of a list of raw log strings. What's the Python one-liner that shouts at you?
A list comprehension. Something like [line for line in logs if "ERROR" in line]. Filter every line where the word appears.
Almost. There's a subtle bug hiding in that. What if a message contains the literal text "NO ERROR FOUND"?
[line for line in logs if "ERROR" in line]That pattern matches any line where ERROR appears anywhere — including inside a message field.
So I need to check that ERROR is the level field, not just a substring. How?
Wrap the level in spaces. In our log format the level is always surrounded by whitespace — one space after the timestamp and one before the message. Search for " ERROR " with spaces on both sides:
[line for line in logs if " ERROR " in line]Now NO ERROR FOUND in the message won't match — the surrounding context rules it out.
Can I parameterise that? I want one function that filters by any level, not just ERROR.
Build the search string dynamically with an f-string: search = f" {level} ". Then the comprehension uses if search in line. One function handles ERROR, WARN, INFO — anything the caller hands in.
Fast path, no parsing needed. I only reach for the full parser when I need the structured dict.
Exactly the DevOps instinct. Use the cheap tool when the format is predictable. Reserve the expensive tool for when the structure might shift.
TL;DR: [x for x in items if condition] is the Python idiom for filtering — always prefer it over a manual for loop with append.
in operator — membership check, case-sensitive| Pattern | Matches "... ERROR ..." | Matches "... NO ERROR FOUND ..." |
|---|---|---|
"ERROR" | yes | yes (false positive) |
" ERROR " | yes | no |
You want to pull just the ERROR lines out of a list of raw log strings. What's the Python one-liner that shouts at you?
A list comprehension. Something like [line for line in logs if "ERROR" in line]. Filter every line where the word appears.
Almost. There's a subtle bug hiding in that. What if a message contains the literal text "NO ERROR FOUND"?
[line for line in logs if "ERROR" in line]That pattern matches any line where ERROR appears anywhere — including inside a message field.
So I need to check that ERROR is the level field, not just a substring. How?
Wrap the level in spaces. In our log format the level is always surrounded by whitespace — one space after the timestamp and one before the message. Search for " ERROR " with spaces on both sides:
[line for line in logs if " ERROR " in line]Now NO ERROR FOUND in the message won't match — the surrounding context rules it out.
Can I parameterise that? I want one function that filters by any level, not just ERROR.
Build the search string dynamically with an f-string: search = f" {level} ". Then the comprehension uses if search in line. One function handles ERROR, WARN, INFO — anything the caller hands in.
Fast path, no parsing needed. I only reach for the full parser when I need the structured dict.
Exactly the DevOps instinct. Use the cheap tool when the format is predictable. Reserve the expensive tool for when the structure might shift.
TL;DR: [x for x in items if condition] is the Python idiom for filtering — always prefer it over a manual for loop with append.
in operator — membership check, case-sensitive| Pattern | Matches "... ERROR ..." | Matches "... NO ERROR FOUND ..." |
|---|---|---|
"ERROR" | yes | yes (false positive) |
" ERROR " | yes | no |
Write `filter_by_level(logs, level)` that takes a list of raw log strings and a level name. Return only the lines where the level appears surrounded by spaces — like `" ERROR "` — using a list comprehension.
Tap each step for scaffolded hints.
No blank-editor panic.