I was wondering why when counting the number of columns in python I would use the code:
ROWS = len(table[0])
Thanks!
I was wondering why when counting the number of columns in python I would use the code:
ROWS = len(table[0])
Thanks!
The 0 index means you're looking at the first item of the list table
. If table is a 3D list as shown below, then you can clearly see in this representation that the number of columns is equal to the length of the first list ([1,2,3]), which is 3 columns.
[
[1,2,3],
[4,5,6],
[7,8,9]
]
table[0]
returns you first row, and len
returns you it's length. Since inside a table all rows have same number of elements, it doesn't matter which row to count - hence, counting first row ([0]
) does the job!