Python List Comprehension With If/Else (Code Examples)
Use if else inside a Python list comprehension to transform elements, or a trailing if to filter them, with examples combining and nesting both.
Read guidePractical, example-led guides for solving real problems with spreadsheets, Python and business automation.
# Turn a repetitive task into a script
from pathlib import Path
files = Path("reports").glob("*.csv")
for report in files:
clean(report)
print(f"✓ {report.name}")
# Small script. Big time saved.Use if else inside a Python list comprehension to transform elements, or a trailing if to filter them, with examples combining and nesting both.
Read guideCombine multiple XPath conditions with and, stacked predicates, not() and grouped or expressions, with attribute, text and Selenium examples.
Read guideFilter blank and non-blank rows in Google Sheets QUERY, including is null, is not null, empty strings and formulas that display blank values.
Read guide