Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the x = [m]*n syntax in Python?

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. What is this called?

>>> x = [0]*7
>>> x
[0, 0, 0, 0, 0, 0, 0]
like image 306
steven smith Avatar asked May 15 '16 05:05

steven smith


People also ask

What does 0 ]* n mean in Python?

X = [0] * N creates an array of zeros of N length. For example: >>> [0] * 10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ** is the power operator.

What is the meaning of X [:- 1 in Python?

Meaning of X = X[:, 1] in Python is: X is a dataset or a array. Say Here X have n rows and n columns. so by doing x=x[:,1] we get all the rows in x present at index 1.

What is M and N in Python?

Practical Data Science using Python Here n is given value which is positive number m is the number of times till which the series run.

What does X mean in Python?

X is a variable name, so could be any name allowed by python for variable names. As a variable , it's value will be different every time the loop circle ends, in this particular loop range(10) the value of x will start in 0 and next 1 , and next 2, until reach value of 10.


2 Answers

* is just a multiplication - as + for lists is an intuitive thing, meaning concatenate both operands, the next step is multiplication by a scalar - with [0] * N meaning "concatenate this list with itself N times"!

In other words: * is an operator defined in Python for its primitive sequence types and an integer to concatenate the sequence with itself that number of times. It will work with lists, tuples and even strings.

That is somewhat natural in Python also because the language allows for operator overloading - so Python programmers do expect operators to do meaningful things with objects.

One should take some care that the objects that compose the resulting list are not copies of the objects on the original list - but references to the same object. Thus, if the contents of the original list were just numbers or some other immutable object, there are no surprises - but if it contains mutable objects, such as inner lists, one could be struck by severe side effects when changing them - like in this snippet:

In [167]: a = [[0]] * 7

In [168]: a
Out[168]: [[0], [0], [0], [0], [0], [0], [0]]

In [169]: a[0].append(1)

In [170]: a
Out[170]: [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]
like image 130
jsbueno Avatar answered Oct 30 '22 15:10

jsbueno


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.

like image 39
miradulo Avatar answered Oct 30 '22 16:10

miradulo