This should translate directly into a list-comprehension:
clamp_sel = [int(clamp_normalized[i][j][0])
for i in range(0, len(clamp_normalized))
for j in range(0, len(clamp_normalized[i]))
if clamp_normalized[i][j][0] < int(max_min_band[index_selec])]
The general rule is (see the manual) that you should write the list-comprehension in exactly the same order as you would do with a series of nested for loops and if-statements. The only thing you change is to replace the final xx.append(yy)
with just yy
at the front of the list comprehension. Also note that this is essentially one long expression that you could write on an extremely long line. Because of the enclosing []
, you can divide this expression over multiple lines, with arbitrary indentation.
If the list-comprehension is more pythonic than the original is a question of taste. In this case, the nesting is straightforward, so I would personally go for the list-comprehension. If it gets more complex, stick to the for-loops.
As thefourtheye shows, this example can be further simplified by replacing the use of range()
with a direct iteration over your lists.