How to add, say, n entries x to the list in one shot?

For example, say a list L = [0,1,2,3], and I want to add 10 items from 4:

L=[0,1,2,3,4,4,4,4,4,4,4,4,4,4] 

without using a loop or anything

+5
source share
2 answers

This is quite simple due to the fact that you can add and / or multiply lists:

L += [4] * 10

Here is the proof:

>>> L = [0,1,2,3]
>>> L += [4] * 10
>>> L
[0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
+9
source
L.extend([4] * 10)

L.extend([some_mutable_object for x in range(10)])
+2
source

All Articles