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,851 questions

51,772 answers

573 users

How to declare, initialize and print two-dimensional (2D) list (array) of integers in Python

3 Answers

0 votes
from sys import stdout


def print_list(list2d, n):
    for ii in range(n):
        for jj in range(n):
            stdout.write("%4d" % list2d[ii][jj])
        stdout.write("\n")


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

print_list(a, size)

'''
run:

   1   8   5
   6   7   1
   8   7   6

'''

 



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


def print_list(list2d, n):
    for ii in range(n):
        for jj in range(n):
            stdout.write("%4d" % list2d[ii][jj])
        stdout.write("\n")


def init_list(list2d, n):
    for ii in range(n):
        for jj in range(n):
            list2d[ii][jj] = random.randint(2, 9)


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

print_list(a, size)
init_list(a, size)
print()
print_list(a, size)

'''
run:

   0   0   0
   0   0   0
   0   0   0

   8   2   2
   7   5   8
   6   3   7

'''

 



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


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")


def init_list(list2d):
    for ii in range(len(list2d)):  # rows
        for jj in range(len(list2d[0])):  # columns
            list2d[ii][jj] = random.randint(2, 9)


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

print_list(a)
init_list(a)
print()
print_list(a)

'''
run:

   0   0   0
   0   0   0

   7   5   8
   9   7   6

'''

 



answered Mar 1, 2016 by avibootz
edited Mar 1, 2016 by avibootz
...