I need to do the following using list comprehension
ans = []
for i in range(0, x):
l = []
for j in range(0, y):
l.append(i*j)
ans.append(l)
I need to do the following using list comprehension
ans = []
for i in range(0, x):
l = []
for j in range(0, y):
l.append(i*j)
ans.append(l)
Here's a list comprehension that is equivalent to your nested loops:
a = [[i * j for j in range(0, y)] for i in range(0, x)]
Since you want individual list:
ans = [[i*j for j in range(y)] for i in range(x)]
If you want it flattened:
ans = [i*j for i in range(x) for j in range(y)]