'''lecture_18_nested_loops.py Demonstrate using nested lists Oliver W. Layton CS 151: Computational Thinking: Visual Media Fall 2020 ''' ''' Visiting every item in a list with sublists ''' # How can we access and replace every item in `digits` # with 10 times the number? digits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # What is len(digits)? # print(len(digits)) # If we loop thru, what type of data is digits[i]? # for i in range(len(digits)): # print(digits[i]) # To visit each int, as we loop through the list, # let's start looping thru the sublist and print each item. # for i in range(len(digits)): # # inside "i" loop over sublists. Now we loop thru the SUBLIST digits[i] and get single items out # # digits[i] is a sublist — e.g. [1, 2, 3] # for j in range(len(digits[i])): # print(digits[i][j]) # Now let's tackle the problem of replacing each number with 10 times # the amount. for i in range(len(digits)): for j in range(len(digits[i])): digits[i][j] = 10*digits[i][j] print(digits) # What values do `i` and `j` take on while iterating thru the nested loop? # Let's print the see for i in range(len(digits)): for j in range(len(digits[i])): print('i =', i, ', j =', j) print('--Done with inner loop!--')