List the understanding of salvation!
foo = [{} for _ in range(n)]
I am not afraid of short notes. In Python 2, you use xrange(n) instead of range(n) to avoid materializing a useless list.
Alternative [{}] * n creates a list of length n with only one dictionary referenced n times. This leads to unpleasant surprises when adding keys to the dictionary.
Martijn pieters
source share