'''lecture_18_sublists.py Demonstrate how to work with sublists Oliver W. Layton CS151: Computational Thinking: Visual Media Fall 2020 ''' ''' Python allows us to store sublists: lists inside lists: ''' digits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Might be easier to see the "items" when written the following way: # (but often not used because more effort to write) digits = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]] # What is the length of digits (number of items)? print(len(digits)) # How do we access the second sublist (`[4, 5, 6]`)? print(digits[1]) # How do we access the 1st two sublists `[[1, 2, 3], [4, 5, 6]]`? print(digits[:2]) ''' Multiple indexing ''' # How do we access/address the individual ints rather than the lists? # Need multiple nexting notation: double set of square brackets # Example: digits[0][1] takes the first item of the list of lists # (a list), then from that sublist it takes the 2nd item (at index 1): print(digits[0][2]) # When written like the following, you can think of a list of lists # like a matrix or table: `digits[][column number>]` digits = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]