Python initialize large list with same value

To initialize a large list with same value (all items having same value), the following syntax can be used.

a = [val] * num_items

Example

a = [0]*20
print a

a = [None]*20
print a
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
Env: Python 2.7.6
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments