steven smith steven smith - 11 months ago 52
Python Question

python what is x = [m]*n syntax

I stumbled on the 'x = [m]*n' and running it in the interpreter I can see that the code allocates an n element array initialized with m. But I can't find a description of this type of code online. Can someone tell me what this is called?

>>> x = [0]*7
>>> x
[0, 0, 0, 0, 0, 0, 0]


From the Python docs' description, the multiplication operator * used between an integer n and a primitive sequence type performs sequence repetition of the items in the sequence n times. So I suppose the term you are looking for is sequence repetition. Note that this is not "sequence copying", as no copies of the items are created - you have n references to the very same sequence.