-3

I was wondering why when counting the number of columns in python I would use the code:

ROWS = len(table[0])

Thanks!

  • What does the 0 index signify? – SadTacoMadTaco Apr 02 '20 at 06:43
  • 0 index signifies the first row of the table/dataframe. Therefore, `table[0]` is the series of all column values of the first row whose `len()` will give the number of column present in the table – Vanshika Apr 02 '20 at 06:47
  • Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Tristen Apr 02 '20 at 06:51

2 Answers2

0

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]
]
incarnadine
  • 658
  • 7
  • 19
0

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!

avloss
  • 2,389
  • 2
  • 22
  • 26