Python create array of certain depth

So basically what i want my script to do is output something like

somelist = ([0,0],[0,0])

given x = 2. so if x were 3 it would be

somelist = ([0,0,0],[0,0,0],[0,0,0])

what I've come up with so far is

somelist = ([0] * x) * x

but this simply returns

somelist = [0,0,0,0]

Anybody know of a simple way to do this?

1 answer

  • answered 2017-11-12 19:29 schwobaseggl

    The old singleton tuple issue. Add a comma or else the parentheses will be meaningless (interpreted merely as precedence/grouping markers):

    somelist = ([0] * x,) * x
    #                  ^--- makes all the difference