Length of rows and columns

My Code:

def explore_data(dataset, start, end, rows_and_columns=False):
    dataset_slice = dataset[start:end]    
    for row in dataset_slice:
        print(row)
        print('\n') 
        
    if rows_and_columns:
        print('Number of rows:', len(dataset))
        print('Number of columns:', len(dataset)[0]))
        
print(android_header)
print('\n')
explore_data(android, 0, 3, True)

How does the program know that ‘len(dataset)’ is the length of the rows and that ‘len(dataset[0]’ is the length of the columns?

Hi @s.cook20:

Kindly refer to my illustration below.

dataset[0] refers to the header row of the dataset. Recall that python is zero-indexed thus indexing starts at 0. The red outline illustrates all the elements in dataset[0]. Thus, len(dataset[0]) gives the number of columns (in this case 6, again since python is zero indexed–column 0, which is the company name to column 6–the URL).

Therefore dataset[1] refers to the next row of values after the header as illustrated by the green box.

len(dataset) refers to the total number of rows in the dataset since a slice is not specified.

In this case, after the csv file is extracted, it would look something like this:

company data = [ [ "Company Name", "Address: Street 1", "Address: Street 2", 
"Address: State", "Address: Zip", "URL"] , 
["Dodgit", "334 Melo Drive", "", "Menlo", "CA", "84432". "http://dodgit.com"] , 
....]

Note that the data is a 2D array (or a list of lists) since it contains both rows and columns. A comma (or sometimes \n) seperates the rows. Thus, len(dataset) counts the number of such rows or 1D arrays, while len(dataset[0]) counts the individual elements in the array.

Hope this helps!

1 Like

Makes sense, thanks!

No worries @s.cook20. Happy learning!