Skip to content
Advertisement

loop in a list of lists

I am trying to use two foor loops for a variable with that has list within a list, but this code doesn’t work. I get the error: list index out of range

JavaScript

Advertisement

Answer

In your loop, j is not an index, it’s the element, you can use range to loop over the indices (same thing about i, use i[j], not D[i][j], since i is a list):

JavaScript

Alternatively, you can use a list comprehension:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement