Home > database >  how can ı one line create list with for loop not tuple pyhton
how can ı one line create list with for loop not tuple pyhton

Time:08-12

a =[[(i,a) for i in range(0,41,20) for a in range(0,41,20)]]
print(a)

That's print list = [(0,20), (0,40) or something going but ı want [[0,20],[0,40] something like that. I don't want tuple and ı want one line code with for loop.

CodePudding user response:

(i,a) creates a tuple. Just use [i, a] instead:

out = [[[i, a] for i in range(0, 41, 20) for a in range(0, 41, 20)]]
print(out)

Output:

[[[0, 0], [0, 20], [0, 40], [20, 0], [20, 20], [20, 40], [40, 0], [40, 20], [40, 40]]]

CodePudding user response:

a =[[[i,a] for i in range(0,41,20) for a in range(0,41,20)]]
print(a)

Output: [[[0, 0], [0, 20], [0, 40], [20, 0], [20, 20], [20, 40], [40, 0], [40, 20], [40, 40]]]

[[i,a] for i in range(0,41,20) for a in range(0,41,20)]

Remove one [] to get this [[0,20],[0,40] something like that

  • Related