I want to write a function that reads a list [1,5,3,6,...]
and gives [1,1,5,5,3,3,6,6,...]
.
Any idea how to do it?

- 12,024
- 2
- 30
- 47

- 2,637
- 4
- 20
- 12
-
6Sounds homeworkish. There are better ways to work with a list than duplicating the elements. – S.Lott Mar 15 '10 at 17:28
-
2I know this question is over 10 years old, but I really hate these "sounds homeworkish" comments. Who cares where the requirement came from? An example where this is used in the "real world" is, for example, where you need increase the size of data for testing when you only have a preliminatry sample but need to see how it performs on bigger lists. – cdabel Apr 30 '21 at 20:32
12 Answers
>>> a = range(10)
>>> [val for val in a for _ in (0, 1)]
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]
N.B. _
is traditionally used as a placeholder variable name where you do not want to do anything with the contents of the variable. In this case it is just used to generate two values for every time round the outer loop.
To turn this from a list into a generator replace the square brackets with round brackets.

- 25,806
- 5
- 67
- 84
-
1`_` is, these days, used for i18n/l10n (google). I still tend to use it *if* I know there won't be an i18n in this module. Else I (would) use `__` (two underscores). – Jürgen A. Erhard Mar 15 '10 at 17:59
-
13
-
1@M.I.Wright I tried `for _ in (_, _)` to counter your clarity, but fortunately it does not work. – Guimoute May 01 '20 at 14:16
>>> a = [1, 2, 3]
>>> b = []
>>> for i in a:
b.extend([i, i])
>>> b
[1, 1, 2, 2, 3, 3]
or
>>> [a[i//2] for i in range(len(a)*2)]
[1, 1, 2, 2, 3, 3]

- 307,395
- 66
- 306
- 293
-
-
@Mike: sure you right, except of course in `/` division insures that `int` is returned. – SilentGhost Mar 15 '10 at 17:38
numpy.repeat
does what you want:
import numpy as np
yourList = [1,5,3,6]
n = 2
list(np.repeat(yourList, n))
result:
[1, 1, 5, 5, 3, 3, 6, 6]
If you don't mind using numpy arrays you can also omit the list()
call in the last line.

- 2,348
- 5
- 21
- 43
I would use zip
and itertools.chain
.
>>> import itertools
>>> l = [1,5,3,6,16]
>>> list(itertools.chain(*zip(l,l)))
[1, 1, 5, 5, 3, 3, 6, 6, 16, 16]
Note: I only used list
to consume the generator to make it fit for printing. You probably don't need the list
call in your code...

- 112,638
- 29
- 165
- 179
It is possible use list multiplication. Case you need each list member together just use sorted method.
>>> lst = [1,2,3,4]
>>> sorted(lst*2)
[1,1,2,2,3,3,4,4]

- 896
- 6
- 10
-
9What about if you want to keep the order of the original list? What about if the the items in the list are unorderable? – Moberg Feb 19 '18 at 13:40
-
With a little slicing...
>>> a = [3, 1, 4, 1, 5]
>>> a[:0] = a[::2] = a[1::2] = a[:]
>>> a
[3, 3, 1, 1, 4, 4, 1, 1, 5, 5]

- 27,593
- 8
- 44
- 107
-
1It works, but can you explain why? For instance inspecting `a[1::2] = a[:]` yields `ValueError: attempt to assign sequence of size 5 to extended slice of size 2`. – Friedrich -- Слава Україні Jun 19 '20 at 18:56
-
1
-
1@Friedrich The targets get assigned [left to right](https://docs.python.org/3/reference/simple_stmts.html#grammar-token-assignment-stmt), so `a[:0]` gets assigned first and this resizes `a` as needed for the following two assignments to work. – Stefan Pochmann Jun 19 '20 at 23:07
-
I provided an explanation of this hack here: https://stackoverflow.com/a/67241399/2111778 – xjcl Apr 29 '21 at 09:34
I would use
import itertools
foo = [1, 5, 3, 6]
new = itertools.chain.from_iterable([item, item] for item in foo)
new
will be an iterator that lazily iterates over the duplicated items. If you need the actual list computed, you can do list(new)
or use one of the other solutions.

- 73,987
- 14
- 101
- 130
-
2or shorter: `itertools.chain.from_iterable(itertools.izip(foo, foo))` – Antony Hatchkins Mar 15 '10 at 17:56
-
I considered that code which is shorter but didn't seem clearer to me. – Mike Graham Mar 15 '10 at 18:34
One can use zip and flat the list
a = [3, 1, 4, 1, 5]
sum(zip(a,a), ()) # (3, 3, 1, 1, 4, 4, 1, 1, 5, 5)
The output is a tuple, but conversion to a list is easy.
Regarding flatting a tuple with sum
see https://stackoverflow.com/a/952946/11769765 and python: flat zip.

- 2,901
- 1
- 21
- 40
For as much as Guido dislikes the functional operators, they can be pretty darned handy:
>>> from operator import add
>>> a = range(10)
>>> b = reduce(add, [(x,x) for x in a])

- 30,189
- 5
- 49
- 65
-
In the case of reduce, handy often means amazingly slow. It's important to measure what `reduce` is doing. Often, it's shocking how much computation reduce forces. – S.Lott Mar 15 '10 at 19:40
-
I made a test script with every one of the methods on this page with the baselist = range(10) and 1,000,000 iterations. The slowest took 5.094 seconds and the fastest took 3.622 seconds. My reduce example took 3.906 seconds. – Kirk Strauser Mar 15 '10 at 20:05
-
1`range(10)` is tiny, so the complexity pays a small role. This solution is quadratic; all the others I see here are linear. Also, some of the others seem more readable to me. – Mike Graham Mar 15 '10 at 20:57
For a more general approach you could go with a list comprehension and a factor term.
Example
sample_list = [1,2,3,4,5]
factor = 2
new_list = [entry for entry in sample_list for _ in range(factor)]
Out:
>>> new_list
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
Changing the factor
variable will change how many entry of each item in the list you will have in the new list.
You could also wrap it up in a function:
def multiply_list_entries(list_, factor = 1):
list_multiplied = [entry for entry in list_ for _ in range(factor)]
return list_multiplied
>>> multiply_list_entries(sample_list, factor = 3)
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5]

- 1,029
- 11
- 22
ls1=[1,2,3]
ls2=[]
for i in ls1:
ls2.append(i)
ls2.append(i)
This code duplicates each elements in ls1 the result ls2 --> [1,1,2,2,3,3]

- 1
- 1
-
1Can you please explain your answer so that when someone has this issue they can understand it as well? Thanks! – Sep 19 '21 at 01:44
-
this answer was flagged for low quality, please better the answer with more supporting info and explanation or delete it – Noam Yizraeli Sep 19 '21 at 09:33