Python Language List Initializing a List to a Fixed Number of Elements

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

For immutable elements (e.g. None, string literals etc.):

my_list = [None] * 10
my_list = ['test'] * 10

For mutable elements, the same construct will result in all elements of the list referring to the same object, for example, for a set:

>>> my_list=[{1}] * 10
>>> print(my_list)
[{1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}]
>>> my_list[0].add(2)
>>> print(my_list)
[{1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}]

Instead, to initialize the list with a fixed number of different mutable objects, use:

my_list=[{1} for _ in range(10)]


Got any Python Language Question?