From 9f50a98ae6b88e45f44c37d9aea6f1a9e7e7ee1b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 27 May 2026 09:02:05 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20list=20comprehen?= =?UTF-8?q?sions=20with=20walrus=20operator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: thirdeyenation <133812267+thirdeyenation@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ helpers/dirty_json.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..954473e674 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,4 @@ + +## 2024-05-24 - Optimize list comprehensions with walrus operator + **Learning:** List comprehensions with redundant function calls (like `input_str.find(char)`) cause the function to be evaluated twice, which is an anti-pattern that slows down processing, especially for O(n) string scanning. + **Action:** Use the Python 3.8+ walrus operator (`:=`) to compute and bind the result once, e.g., `[idx for char in chars if (idx := input_str.find(char)) != -1]`. diff --git a/helpers/dirty_json.py b/helpers/dirty_json.py index 8b731bee47..ab099bf784 100644 --- a/helpers/dirty_json.py +++ b/helpers/dirty_json.py @@ -349,5 +349,5 @@ def _peek(self, n): def get_start_pos(self, input_str: str) -> int: chars = ["{", "[", '"'] - indices = [input_str.find(char) for char in chars if input_str.find(char) != -1] + indices = [idx for char in chars if (idx := input_str.find(char)) != -1] return min(indices) if indices else 0