Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,788 questions

51,694 answers

573 users

How to define, use and print 2D (two-dimensional) array (list) in Python

4 Answers

0 votes
w, h = 3, 2

arr = []
for y in range(h):
    arr.append([0 for x in range(w)])

arr[0][0] = 3
arr[0][1] = 8
arr[0][2] = 1

arr[1][0] = 4
arr[1][1] = 6
arr[1][2] = 9

print(arr[0][0])
print(arr[1][1])
print(arr[1][2])


'''
run:

3
6
9

'''

 



answered Dec 3, 2016 by avibootz
0 votes
w, h = 3, 2

arr = []
for y in range(h):
    arr.append([0 for x in range(w)])

arr[0][0] = 3
arr[0][1] = 8
arr[0][2] = 1

arr[1][0] = 4
arr[1][1] = 6
arr[1][2] = 9

print('\n'.join([''.join(['{:4}'.format(item) for item in row]) for row in arr]))


'''
run:

   3   8   1
   4   6   9

'''

 



answered Dec 3, 2016 by avibootz
0 votes
w, h = 3, 2

arr = []
for y in range(h):
    arr.append([0 for x in range(w)])

arr[0][0] = 3
arr[0][1] = 8
arr[0][2] = 1

arr[1][0] = 4
arr[1][1] = 6
arr[1][2] = 9

for i in range(h):
    for j in range(w):
        print('{:4}'.format(arr[i][j]), end=" ")
    print('\n')


'''
run:

   3    8    1

   4    6    9 

'''

 



answered Dec 3, 2016 by avibootz
0 votes
w, h = 3, 2

arr = []
for y in range(h):
    arr.append([0 for x in range(w)])

arr[0][0] = 3
arr[0][1] = 8
arr[0][2] = 1

arr[1][0] = 4
arr[1][1] = 6
arr[1][2] = 9

for row in arr:
    for val in row:
        print('{:4}'.format(val), end=" ")
    print('\n')


'''
run:

   3    8    1

   4    6    9 

'''

 



answered Dec 3, 2016 by avibootz
...