Duplicate each member in a list

PythonListDuplicates

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

>>> 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.

Solution 2 - Python

>>> 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]

Solution 3 - Python

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.

Solution 4 - Python

If you already have the roundrobin recipe described in the documentation for itertools—and it is quite handy—then you can just use

roundrobin(my_list, my_list)

Solution 5 - Python

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...

Solution 6 - Python

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]

Solution 7 - Python

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]

Solution 8 - Python

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.

Solution 9 - Python

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 https://stackoverflow.com/questions/61943924/python-flat-zip/61943958#61943958.

Solution 10 - Python

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])

Solution 11 - Python

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]

Solution 12 - Python

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]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarielView Question on Stackoverflow
Solution 1 - PythonDave KirbyView Answer on Stackoverflow
Solution 2 - PythonSilentGhostView Answer on Stackoverflow
Solution 3 - PythonPDiracDeltaView Answer on Stackoverflow
Solution 4 - PythonHank GayView Answer on Stackoverflow
Solution 5 - PythonChristopheDView Answer on Stackoverflow
Solution 6 - PythonStefan PochmannView Answer on Stackoverflow
Solution 7 - PythonmmachineView Answer on Stackoverflow
Solution 8 - PythonMike GrahamView Answer on Stackoverflow
Solution 9 - PythonFriedrichView Answer on Stackoverflow
Solution 10 - PythonKirk StrauserView Answer on Stackoverflow
Solution 11 - PythonAeliusView Answer on Stackoverflow
Solution 12 - Pythonduoduo_jzView Answer on Stackoverflow