From 576a3f8a31ff96a8e25b4f32b59e3d7dc1189864 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 28 Feb 2020 10:11:17 +0000 Subject: [PATCH 1/2] add a note on extracting multiline processing in comprehensions --- ...r_where_list_comprehension_is_possible.rst | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst b/docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst index f5c143f..a24ee85 100644 --- a/docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst +++ b/docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst @@ -1,7 +1,8 @@ -Using ``map()`` or ``filter()`` where list comprehension is possible -==================================================================== +Using ``map()`` or ``filter()`` +=============================== + +For transformations that can be expressed as a list comprehension, use list comprehensions over ``map()`` or ``filter()``. Although a ``map()`` or ``filter()`` expression may be functionally equivalent to a list comprehension, the list comprehension is more concise and easier to read. For expressions that are too long or complicated to express directly within a comprehension extract them into a function. -For simple transformations that can be expressed as a list comprehension, use list comprehensions over ``map()`` or ``filter()``. Although a ``map()`` or ``filter()`` expression may be functionally equivalent to a list comprehension, the list comprehension is generally more concise and easier to read. Anti-pattern ------------ @@ -26,6 +27,20 @@ In the modified code below, the code uses a list comprehension to generate the s values = [1, 2, 3] doubles = [x * 2 for x in values] +Sometimes a condition or process is too complicated or impossible to express in a single expression, for those extract them to a function and use a comprehension: + +.. code:: python + + def process(v): + if v.ham == "spam": + return 4 + if v.bacon == "eggs" + return 7 + + def cond(v): ... + + [process(x) for x in items if cond(v)] # preferable to map(process, filter(cond, items)) + References ---------- From 7ce8abad4bd4493c36b00799463c3667c6a4d3ab Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 28 Feb 2020 10:12:49 +0000 Subject: [PATCH 2/2] Update docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst --- ...sing_map_or_filter_where_list_comprehension_is_possible.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst b/docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst index a24ee85..1371a99 100644 --- a/docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst +++ b/docs/readability/using_map_or_filter_where_list_comprehension_is_possible.rst @@ -35,7 +35,7 @@ Sometimes a condition or process is too complicated or impossible to express in if v.ham == "spam": return 4 if v.bacon == "eggs" - return 7 + return 7 def cond(v): ... @@ -47,4 +47,3 @@ References - PyLint - W0110, deprecated-lambda - `Oliver Fromme - List Comprehensions `_ -