1 / 12

120 likes | 125 Views

Learn how to use 2D arrays or lists of lists in Python to store matrix-like data such as graphs or charts. Explore functions like len, append, and looping through 2D arrays.

Download Presentation
## Introduction to Computer Science

**An Image/Link below is provided (as is) to download presentation**
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.
Content is provided to you AS IS for your information and personal use only.
Download presentation by click this link.
While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

**Introduction to Computer Science**Programming with Python**List of Lists**• A list in Python can contain multiple lists • A list of lists in other programming languages is known as 2D Array/List • It is very useful to store Matrix-like data such as graphs, charts, etc • 2 Dimensional Array**List of Lists**• We can define list of lists as follows Or more human readable a = [[1, 2, 3], [4, 5, 6]] a = [ [1, 2, 3],[4, 5, 6] ] Row 0 Row 1 column 2 column 1 column 3**Length of Rows and Columns**It is possible to get how many rows and columns of the Matrix or list of lists using the len function length of a: 2 (number of rows / lists) 3 columns in row 0 a = [ [1, 2, 3],[4, 5, 6] ]print("length of a: ", len(a)) print(len(a[0]), " columns in row 0")**The append() function with 2D Lists**• Just like lists, we can use the append function to insert data to the list • We can add to an existing list. In other words, add a column to an existing row or add a new row • Remember, lists are mutable so list of lists are also mutable • add 100 to row 0 – this will increate the length of row 0 This will add a new row to the list • This will add a number without a list - do not do it • Instead, add a single value in a row itself / list of 1 value a[0].append(100) a.append([120,200,320]) a.append(100) a.append([100])**Definite Loops with 2D Lists / List of Lists**• It is possible to loop through 2D array using the classic way that works with all programming languages. This is good because it will give you an insight of how to deal with 2 Dimensional Lists/Arrays • Nested for loops • First loop will go through rows • Second loop will go through each column of the row. Or, each element in a specific row**Definite Loops with 2D Lists / List of Lists**a = [ [1, 2, 3],[4, 5, 6],[7, 8, 9, 0] ] for row in range(0, len(a)):for col in range(0, len(a[row])):print(a[row][col], end="\t")print()**Definite Loops with 2D Lists / List of Lists**a = [ [1, 2, 3],[4, 5, 6],[7, 8, 9, 0] ] ? print("element of a[2][3]")**Definite Loops with 2D Lists / List of Lists**a = [ [1, 2, 3],[4, 5, 6],[7, 8, 9, 0] ] print("element of a[2][3]")**Definite Loops with 2D Lists / List of Lists**a = [ [1, 2, 3],[4, 5, 6],[7, 8, 9, 0] ] Debugging table consists of 10 rows (excluding header) for row in range(0, len(a)):for col in range(0, len(a[row])):print(a[row][col], end="\t")print()

More Related