My array is [A,B,C]
and I want to multiply it so I can make the array [A,A,A,B,B,B,C,C,C]
Asked
Active
Viewed 236 times
3

BobelLegend
- 91
- 2
- 10
-
The number to multiply it, will it be same for each element, like 3 or 5 etc.. ? – zenwraight Jul 30 '18 at 20:14
-
1You can just write a for loop to iterate over this and then put the elements. – zenwraight Jul 30 '18 at 20:14
6 Answers
4
you can chain the results of itertools.repeat
:
import itertools
list(itertools.chain.from_iterable(itertools.repeat(x,3) for x in ["A","B","C"]))
result:
['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']
this solution minimizes the loops & the temporary lists (none is created)

Jean-François Fabre
- 137,073
- 23
- 153
- 219
4
Use zip
>>> l = ['A','B','C']
>>> [e for sl in zip(*[l]*3) for e in sl]
['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']
Or you can use itertools.chain
to flatten the list produced by zip
>>> list(chain(*zip(*[l]*3)))
['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']

Sunitha
- 11,777
- 2
- 20
- 23
-
in that case use `list(chain.from_iterable(zip(*[l]*3)))` avoids star unpacking of zip return value – Jean-François Fabre Jul 30 '18 at 20:44
3
If not opposed to numpy
, you can create an array of your original list repeated using np.repeat
:
>>> import numpy as np
>>> np.repeat(['A','B','C'], 3)
array(['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
dtype='<U1')
If so desired, you can convert that back to a list as opposed to an np.array
using tolist
:
>>> np.repeat(['A','B','C'], 3).tolist()
['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']

sacuL
- 49,704
- 8
- 81
- 106
2
You could make use of list comprehensions:
>>> [item for item in ['A', 'B', 'C'] for _ in range(3)]
['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']

Jean-François Fabre
- 137,073
- 23
- 153
- 219

Maurice Meyer
- 17,279
- 4
- 30
- 47
-
`for mul in range(0,3)` => `for mul in range(3)` is shorter – Jean-François Fabre Jul 30 '18 at 20:19
-
1`for mul in range(3)` => `for _ in range(3)` is shorter-er. :P Since `mul` is [unused](https://stackoverflow.com/questions/5477134/how-can-i-get-around-declaring-an-unused-variable-in-a-for-loop/5477153#5477153). – Sunny Patel Jul 30 '18 at 20:26
-
2
Not quite as clean as the other answers, but this should work too:
test = ['A','B','C']
def repeat_elements(currentArray, numRepeat):
newArray = []
for elem in currentArray:
for x in range(numRepeat):
newArray.append(elem)
return newArray
print repeat_elements(test, 3)

Jon Warren
- 857
- 6
- 18
1
This should work for you regardless of how long the list is.
array = ['A','B','C']
new_array = []
for i in array:
new_array += i * 3
(This works as long as you're not using integers in place of characters/strings as array elements)

Dylan Smith
- 118
- 1
- 10
-
The other option, if sorting isn't an issue, is to simply do: new_array = array * 3 – Dylan Smith Jul 30 '18 at 20:22
-
2I think this solution will fail if he uses integers in place of characters/strings as array elements – Jon Warren Jul 30 '18 at 20:26
-
-
1The problem I found with this (I already tried this) was that you have to write [i] and it makes an array of arrays instead of one array. – BobelLegend Jul 30 '18 at 20:41