Is there any way to use list comprehension in this case ?
l=[]
for i in range(-1,2):
for j in range(-1,2):
l.append([i,j])
Output:
[[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
Is there any way to use list comprehension in this case ?
l=[]
for i in range(-1,2):
for j in range(-1,2):
l.append([i,j])
Output:
[[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
You can use:
l = [[i, j] for i in range(-1, 2) for j in range(-1, 2)]
Result:
# print(l)
[[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]