Repeating Elements of a List N Times

Create list of single item repeated N times

You can also write:

[e] * n

You should note that if e is for example an empty list you get a list with n references to the same list, not n independent empty lists.

Performance testing

At first glance it seems that repeat is the fastest way to create a list with n identical elements:

>>> timeit.timeit('itertools.repeat(0, 10)', 'import itertools', number = 1000000)
0.37095273281943264
>>> timeit.timeit('[0] * 10', 'import itertools', number = 1000000)
0.5577236771712819

But wait - it's not a fair test...

>>> itertools.repeat(0, 10)
repeat(0, 10) # Not a list!!!

The function itertools.repeat doesn't actually create the list, it just creates an object that can be used to create a list if you wish! Let's try that again, but converting to a list:

>>> timeit.timeit('list(itertools.repeat(0, 10))', 'import itertools', number = 1000000)
1.7508119747063233

So if you want a list, use [e] * n. If you want to generate the elements lazily, use repeat.

Repeating elements of a list n times

In case you really want result as list, and generator is not sufficient:

import itertools
lst = range(1,5)
list(itertools.chain.from_iterable(itertools.repeat(x, 3) for x in lst))

Out[8]: [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

Repeat same element n times in a list

You can use replicate with simplify = FALSE :

e <- 1:5
n <- 5
list_new <- replicate(n, e, simplify = FALSE)
list_new
#[[1]]
#[1] 1 2 3 4 5

#[[2]]
#[1] 1 2 3 4 5

#[[3]]
#[1] 1 2 3 4 5

#[[4]]
#[1] 1 2 3 4 5

#[[5]]
#[1] 1 2 3 4 5

This is the same output as list(e, e, e, e, e).

purrr also has rerun function which gives the similar behaviour.

list_new <- purrr::rerun(n, e)

How to repeat elements in list n times?

An inner argument to repeat is what I was looking for:

repeat([1, 2, 3, 4], inner = 3)

How to repeat each of a Python list's elements n times with itertools only?

Since you don't want to use list comprehension, following is a pure (+zip) itertools method to do it -

from itertools import chain, repeat

list(chain.from_iterable(zip(*repeat(numbers, 3))))
# [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

Creating a list of mutable items repeated N times

I just figured out I can do this through:

from copy import deepcopy
a = [[1],[2],[3]]
n = 3
b = [deepcopy(val) for _ in range(n) for val in a]

Now if I set b[0][0] = 0, I will get b == [[0],[2],[3],[1],[2],[3],[1],[2],[3]].

Best way to extend a list with itself N times

Eventually I came up with:

[copy.copy(e) for _ in range(N) for e in sourceList]

How to repeat an element in a list by the number of times that is the magnitude of that element

You can use a nested list comprehension to get what you want. You already seem to know what [i]*i does.

def numstack(n):
return [j for i in range(1, n + 1) for j in [i]*i]

print(numstack(4))

Output

[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

From the comments a better approach would be to write the comprehension as return [i for i in range(1, n + 1) for j in range(i)]



Related Topics



Leave a reply



Submit