How to find the length (rows, cols) of 2D list (array) in Python

2 Answers

0 votes
from sys import stdout


def print_list(list2d):
    for ii in range(len(list2d)):  # rows
        for jj in range(len(list2d[0])):  # columns
            stdout.write("%4d" % list2d[ii][jj])
        stdout.write("\n")


a = [[0 for x in range(3)] for x in range(2)]

print('rows:', len(a))
print('columns:', len(a[0]))
print()
print_list(a)

'''
run:

rows: 2
columns: 3

   0   0   0
   0   0   0

'''

 



answered Mar 1, 2016 by avibootz
0 votes
from sys import stdout


def print_list(list2d):
    for ii in range(len(list2d)):  # rows
        for jj in range(len(list2d[0])):  # columns
            stdout.write("%4d" % list2d[ii][jj])
        stdout.write("\n")


a = [[1, 8, 5], [6, 7, 1]]

print('rows:', len(a))
print('columns:', len(a[0]))
print()
print_list(a)

'''
run:

rows: 2
columns: 3

   1   8   5
   6   7   1

'''

 



answered Mar 1, 2016 by avibootz

Related questions

1 answer 176 views
4 answers 343 views
4 answers 290 views
1 answer 182 views
2 answers 285 views
...