The "if" keyword in list comprehensions (vs the normal if - else)

Опубликовано: 19 Ноябрь 2024
на канале: PatchyPython
92
0

A special place use of the "if" keyword in Python is within list comprehensions. This so-called "guard" serves to select what elements of the original list or other iterable will be added to the comprehension; it works as a kind of filter. This form of the "if" statement is never followed by an "else". For more information, see the notebooks below (notebooks will open on Google Colab - no installation required)

Embedded video, part of the following courses:
Computer Programming at Queen Mary, University of London
Jupyter notebook: https://tinyurl.com/4umjbjvp
Coding for Scientists at Queen Mary, University of London
Jupyter notebook: https://tinyurl.com/539p4b5f