'''lecture_17_nested_loops.py Demonstrate using nested lists Oliver W. Layton CS 151: Computational Thinking: Visual Media Spring 2021 ''' ''' 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]? # print(digits[0]) # 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)): # currSublist = digits[i] # giving sublist an actual name. # for j in range(len(currSublist)): # same as below # for i in range(len(digits)): # # print(digits[i]) # print('Starting inner loop. i = ', i) # for j in range(len(digits[i])): # get length of current sublist # print(digits[i][j]) # print('Ending inner loop. i = ', i) # 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])): # get length of current sublist 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])): # get length of current sublist print('i = ', i, 'j = ', j)