0

I know I can use the following syntax for loop in one line:

[print(x) for x in range(10)]

How can I use this syntax to combine two loops in one line?

hereiam
  • 1
  • 2

2 Answers2

1

You can use code something like below. It allows iterating by an array of arrays.

x = [[2,5,6],[3,5,1]]
result = [[value[i] for value in x] for i in range(3)]
K.Oleksy
  • 155
  • 1
  • 9
0

[(i, j) for i in range(10) for j in range(10)] for instance or [[i for in range(j)] for j in range(10)] and you can combine those ideas in any way you want

ted
  • 13,596
  • 9
  • 65
  • 107