steven smith steven smith - 1 year ago 92
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]

Answer Source

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.

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