130

This seems like something Python would have a shortcut for. I want to append an item to a list N times, effectively doing this:

l = []
x = 0
for i in range(100):
    l.append(x)

It would seem to me that there should be an "optimized" method for that, something like:

l.append_multiple(x, 100)

Is there?


If you want to append elements from another sequence (instead of the same value repeatedly, you can just .extend with that sequence directly. See How to append to the end of an empty list?.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Toji
  • 33,927
  • 22
  • 105
  • 115
  • 10
    Ended up going with `l.extend([x] * 100)` since it fit my situation best. Amber gets the answer credit for being the most comprehensive. Thanks! – Toji Jan 11 '11 at 05:27
  • 6
    Make sure you understand Python's reference semantics before using this for anything non-trivial. The x's appended to l are all the same x, so mutating one of them affects them all (all one of them, referred to 100 times). – Karl Knechtel Jan 11 '11 at 09:35
  • 1
    For clarification: I am using it for integers, so my usage is safe. – Toji Jan 11 '11 at 14:36

7 Answers7

184

For immutable data types:

l = [0] * 100
# [0, 0, 0, 0, 0, ...]

l = ['foo'] * 100
# ['foo', 'foo', 'foo', 'foo', ...]

For values that are stored by reference and you may wish to modify later (like sub-lists, or dicts):

l = [{} for x in range(100)]

(The reason why the first method is only a good idea for constant values, like ints or strings, is because only a shallow copy is does when using the <list>*<number> syntax, and thus if you did something like [{}]*100, you'd end up with 100 references to the same dictionary - so changing one of them would change them all. Since ints and strings are immutable, this isn't a problem for them.)

If you want to add to an existing list, you can use the extend() method of that list (in conjunction with the generation of a list of things to add via the above techniques):

a = [1,2,3]
b = [4,5,6]
a.extend(b)
# a is now [1,2,3,4,5,6]
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 6
    Or combine both suggestions into the form of `a.extend(b*n)` where `n` is the times you want to repeat elements in b – DarkCygnus Feb 08 '18 at 19:07
41

Use extend to add a list comprehension to the end.

l.extend([x for i in range(100)])

See the Python docs for more information.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Jake
  • 12,713
  • 18
  • 66
  • 96
13

Itertools repeat combined with list extend.

from itertools import repeat
l = []
l.extend(repeat(x, 100))
kevpie
  • 25,206
  • 2
  • 24
  • 28
9

I had to go another route for an assignment but this is what I ended up with.

my_array += ([x] * repeated_times)
CTS_AE
  • 12,987
  • 8
  • 62
  • 63
7
l = []
x = 0
l.extend([x]*100)
Rajan saha Raju
  • 794
  • 7
  • 13
3

You could do this with a list comprehension

l = [x for i in range(10)];
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
0

you can add any value like this for several times:

a = [1,2,3]
b = []
#if you want to add on item 3 times for example:
for i in range(len(a)):
    j = 3
    while j != 0:
        b.append(a[i])
        j-=1
#now b = [1,1,1,2,2,2,3,3,3]