Monday, May 27, 2019

[b]*N Syntax in Python

[b] * N creates a list of size N which contains only b's, where

  • 'b' is a reference to an object, and
  • [b] * N does not make copies of 'b' inside the generated list but simply copies the reference of 'b' to produce the list.

For example, X =[0] * N, produces a list of size N, with all N elements being the value zero.
Thus, X = [0] * 8, produces a list of size 8 and results in X = [0, 0, 0, 0, 0, 0, 0, 0]

The pictorial representation of X = [0] * 8 will be like below:

Be warned that all eight cells of the list reference the same object. This is okay for immutables like integers or strings but may produce unintended results for mutable objects like lists, because of the fact that lists are referential structures in python. If you try to assign a new value to list, say X[2] = 10, this does not technically change the value of the existing integer instance of 0. This computes a new integer, with value 10, and sets cell 2 to reference the newly computed value. Now, hypothetically let's assume an integer is a mutable object, then integer value would have instead changed and all the cells would be pointing to the old reference (holding new value) only. Pictorial representation change due to X[2] = 10 is below:


Thus, for creating a list of lists, the above approach might produce an unintended result as mentioned above because list is a mutable object. For example:
>>> t = [[]] * 5
>>> t [[], [], [], [], []]
>>> t[0].append(5)
>>> t
[[5], [5], [5], [5], [5]]  # Observe value at all indices got changed

So, if you want to create a list of lists properly, one of the approaches could be:
>>> t = []
>>> for i in range(5):
>>>     t.append([])
>>> t[0].append(5)
>>> t
[[5], [], [], [], []]


Solve this question [valid-tic-tac-toe-state] to understand the importance of initializing a list of lists in a proper way with Python.
Reference: StackOverflow

No comments:

Post a Comment