Ali Jawad Fahs Ali Jawad Fahs -4 years ago 72
Python Question

what is the easiest way to create something similar to 2 dimensional array without using numpy in python

in a project I'm supposed to create a Boolean 2 dimensional array that should be initialized to

False
, and the items of this structure should be updated , i don't want to use numpy cause it's a huge code with multi files and this may induce problems.

Answer Source
  • inner array can be created using * (because booleans are immutable)
  • outer array must instanciate inner array as many times as needed to avoid reference to be duplicated: modifying one row would modify all others, that's a common mistake to avoid)

for instance like this:

a = 10
b = 20

l = [[False]*a for _ in range(b)]

(using anonymized variable for loop since it's not used)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download