diff --git a/docs/notes/data-processing/filtering.qmd b/docs/notes/data-processing/filtering.qmd index 95b8c26..4fc1be9 100644 --- a/docs/notes/data-processing/filtering.qmd +++ b/docs/notes/data-processing/filtering.qmd @@ -18,10 +18,7 @@ The simplest way to implement this is by introducing an \"if\" statement into th ```{python} my_numbers = [1, 2, 3, 4, 5, 6, 7] -print("ORIGINAL:", my_numbers) -``` -```{python} for n in my_numbers: if n > 3: # FILTER CONDITION print(n) @@ -32,6 +29,8 @@ We see we are only printing numbers that match the condition. However in this case we lose access to the matching items. To retain access for later, we can implement a familiar collection operation using the `append` method, similar to the [mapping](./mapping.qmd) operation: ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + matching_nums = [] for n in my_numbers: @@ -45,6 +44,8 @@ print(matching_nums) After performing a filter operation, we wind up with a subset of the data. Depending on the condition, is possible for the resulting list to contain no items, one item, some items, or all items: ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + matching_nums = [] for n in my_numbers: @@ -55,6 +56,8 @@ print(matching_nums) ``` ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + matching_nums = [] for n in my_numbers: @@ -65,6 +68,8 @@ print(matching_nums) ``` ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + matching_nums = [] for n in my_numbers: diff --git a/docs/notes/data-processing/list-comprehensions.qmd b/docs/notes/data-processing/list-comprehensions.qmd index f63a344..a494c1a 100644 --- a/docs/notes/data-processing/list-comprehensions.qmd +++ b/docs/notes/data-processing/list-comprehensions.qmd @@ -26,6 +26,8 @@ print(my_numbers) Mapping, the long way: ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + transformed_nums = [] for n in my_numbers: @@ -37,6 +39,8 @@ print(transformed_nums) Mapping, the short way, using a list comprehension (equivalent): ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + # NEW_LIST = [VALUE_TO_COLLECT for ITEM in EXISTING_LIST] transformed_nums = [n * 100 for n in my_numbers] @@ -51,6 +55,8 @@ We optionally add an `if` clause to also implement filtering: Filtering, the long way: ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + matching_nums = [] for n in my_numbers: @@ -63,6 +69,8 @@ print(matching_nums) Filtering, the short way, using a list comprehension (equivalent): ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + # NEW_LIST = [VALUE_TO_COLLECT for ITEM in EXISTING_LIST if CONDITION] matching_nums = [n for n in my_numbers if n > 3] @@ -76,6 +84,8 @@ We can mix and match techniques to perform both mapping and filtering, as desire Filtering and mapping, the long way: ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + matching_nums = [] for n in my_numbers: @@ -88,6 +98,8 @@ print(matching_nums) Filtering and mapping, the short way, using a list comprehension (equivalent): ```{python} +my_numbers = [1, 2, 3, 4, 5, 6, 7] + new_nums = [n * 100 for n in my_numbers if n > 3] print(new_nums) ```