0
How this code works for condition x>y, please explain
l2 = [x * y for x in range( 3 ) for y in range( 3 ) if x>y] print(l2) #prints [0, 0, 2]
2 Réponses
+ 1
The best way to understand list comprehensions is to have a look at the equivalent code written in a traditional way.
Your example can be rewritten as:
l2 = []
for x in range(3):
for y in range(3):
if x > y:
l2.append(x * y)
print(l2)
# [0, 0, 2]
In Python documentation https://docs.python.org/3/tutorial/datastructures.html you will find another example of a list comprehension with double 'for' and 'if' statement.
0
Thank youRadosław Chmielewski