python - I don't really understand how to get the output for this code -
lst = [[2, 3, 4], [1, 1]] sum = 0 in range(len(lst)): j in range(len(lst)): sum += lst[i][j] print(sum)
i'm new python, can explain how output 7
?
both of loops go on range of len(lst)
, while inner 1 should loop on range of len(lst[i])
. real fix this, however, not loop on range(len(...))
loop on elements:
for sub_list in lst: # loop on each list inside of lst value in sub_list: # loop on each number in sub list sum += value
this cleaner, less prone bugs yours, , reads better.
Comments
Post a Comment