The most general answer to this is to use list.extend()
and a generator expression:
l.extend(generate_value() for _ in range(n))
This will add a value n
times. Note that this will evaluate generate_value()
each time, side-stepping issues with mutable values that other answers may have:
>>> [[1]] * 5
[[1], [1], [1], [1], [1]]
>>> _[0].append(1)
>>> _
[[1, 1], [1, 1], [1, 1], [1, 1], [1, 1]]
>>> [[1] for _ in range(5)]
[[1], [1], [1], [1], [1]]
>>> _[0].append(1)
>>> _
[[1, 1], [1], [1], [1], [1]]
When using the multiplication method, you end up with a list of n references to the same list. When you change it, you see the change in every element of the list - as they are all the same.
When using a generator expression or list comprehension, a new list is created for each sub-item, so each item is a different value. Modifying one only affects that one.
Obviously, in your example, the values are immutable, so this doesn't matter - but it's worth remembering for different cases, or if the values might not be immutable.