Multiple Conditional Loops in List Comprehension in Python
A condition can be added to several cycles specified in the inclusion. Let's set a condition so that j
is less than i
:
lst = [(i, j) for i in range(1, 4) for j in range(1, 3) if j < i]
print(lst)
After executing the code, the following list will be returned:
[(2, 1), (3, 1), (3, 2)]
What will be the result of running the following code:
lst = [(i, j) for i in range(0, 5) for j in range(0, 5) if j == i]
print(lst)
What will be the result of running the following code:
lst = [(i, j) for i in range(1, 4) for j in range(1, 4) if i % 2 == 0]
print(lst)