esson 2 ❮ Lesson List ❮ Top Page
❯ 2.5 Looping if Statement
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 6m 6s
☷ Interactive readings 5m
Combining lists and if statements can be very beneficial. By adjusting the indentation properly, we can easily combine both of them.
We can add a condition to the list comprehensions to modify the existing list. The for part has to be written before the if part.
Adding else or multiple ifs are also possible to do in the list comprehension. In this case, the for part has to be written after the else part.
While adding elif is not possible in list comprehension, you can always chain multiple if-else.
However, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.