Alan Wakke Alan Wakke - 1 year ago 101
Python Question

Count rows and columns

How would I go about defining a fuctions in counting the number of rows and columns in a list of lists? For example group1 would be 1 row with 6 columns.

group1 = [['.', 'A', 'A', '.', '.', '.']]

def num_rows(group):

def num_columns(group):

Answer Source

Just check the first index:

def num_rows(group):
    return len(group)

def num_columns(group):
    return len(group[0])

Take in mind this will raise an IndexError exception if there's no rows.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download