49
votes

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?

11
Sounds homeworkish. There are better ways to work with a list than duplicating the elements. - S.Lott
I 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

11 Answers

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

17
votes
>>> 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]
11
votes

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)
10
votes

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

8
votes

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.

5
votes

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

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.

2
votes

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.

1
votes

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

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

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]