Multi-dimensional list initialization

Demian Brecht demianbrecht at
Wed Nov 7 07:56:33 CET 2012

On 2012-11-06, at 5:55 PM, Steven D'Aprano <steve+comp.lang.python at> wrote:
> I'm not entirely sure what your point is here. The OP screwed up -- he 
> didn't generate a 4-dimensional array. He generated a 2-dimensional 
> array. If his intuition about the number of dimensions is so poor, why 
> should his intuition about list multiplication be treated as sacrosanct?

Yep, I may have mis-worded the explanation a bit (although I *did* express that it was a 4D matrix in the OP). I was using a 2D list to represent a 4D matrix in order to easily iterate over 90 degree rotations with zip(*matrix[::-1]). It wasn't for production code (otherwise I *would* be using numpy), it was for an online programming challenge in which external libs are not supported.

> As they say, the only truly intuitive interface is the nipple. There are 
> many places where people's intuition about programming fail. And many 
> places where Fred's intuition is the opposite of Barney's intuition.

I couldn't agree more with this. My question was *not* based on what I perceive to be intuitive (although most of this thread has now seemed to devolve into that and become more of a philosophical debate), but was based on what I thought may have been inconsistent behaviour (which was quickly cleared up with None being immutable and causing it to *seem* that the behaviour was inconsistent to the forgetful mind). As you touch on here, "intuition" is entirely subjective. If you're coming from a C/C++ background, I'd think that your intuition would be that everything's passed by value unless explicitly stated. Someone coming from another background (Lua perhaps?) would likely have entirely different intuition.

> So while I prefer intuitively obvious behaviour where possible, it is not 
> the holy grail, and I am quite happy to give it up.

I fail to see where there has been any giving up on intuitiveness in the context of this particular topic. In my mind, intuitiveness is generally born of repetitiveness and consistency. As everything in Python is a reference, it would seem to me to be inconsistent to treat expressions such as [[obj]*4]*4 un-semantically (Pythonically speaking) and making it *less* intuitive. I agree that Python would definitely be worse off.

Demian Brecht

More information about the Python-list mailing list