Repeat list to max number of elements
Question:
What is the most efficient method to repeat a list up to a max element length?
To take this:
lst = ['one', 'two', 'three']
max_length = 7
And produce this:
final_list = ['one', 'two', 'three', 'one', 'two', 'three', 'one']
See also How to replicate array to specific length array for Numpy-specific methods.
See also Circular list iterator in Python for lazy iteration over such data.
Answers:
I’d probably use iterools.cycle
and itertools.islice
:
>>> from itertools import cycle, islice
>>> lst = [1, 2, 3]
>>> list(islice(cycle(lst), 7))
[1, 2, 3, 1, 2, 3, 1]
Multiply appropriately?
>>> lst = ['one', 'two', 'three']
>>> max_length = 7
>>>
>>> q, r = divmod(max_length, len(lst))
>>> q * lst + lst[:r]
['one', 'two', 'three', 'one', 'two', 'three', 'one']
Benchmarked mine and mgilson’s solution, mine looks more efficient, for example for the below test mine takes about 0.7 seconds while mgilson’s takes about 2.8 seconds.
from timeit import timeit
data = "lst = ['one', 'two', 'three'] * 1000; max_length = 12345678"
print(timeit('q, r = divmod(max_length, len(lst)); q * lst + lst[:r]',
data,
number=10))
print(timeit('list(islice(cycle(lst), max_length))',
data + '; from itertools import cycle, islice',
number=10))
What is the most efficient method to repeat a list up to a max element length?
To take this:
lst = ['one', 'two', 'three']
max_length = 7
And produce this:
final_list = ['one', 'two', 'three', 'one', 'two', 'three', 'one']
See also How to replicate array to specific length array for Numpy-specific methods.
See also Circular list iterator in Python for lazy iteration over such data.
I’d probably use iterools.cycle
and itertools.islice
:
>>> from itertools import cycle, islice
>>> lst = [1, 2, 3]
>>> list(islice(cycle(lst), 7))
[1, 2, 3, 1, 2, 3, 1]
Multiply appropriately?
>>> lst = ['one', 'two', 'three']
>>> max_length = 7
>>>
>>> q, r = divmod(max_length, len(lst))
>>> q * lst + lst[:r]
['one', 'two', 'three', 'one', 'two', 'three', 'one']
Benchmarked mine and mgilson’s solution, mine looks more efficient, for example for the below test mine takes about 0.7 seconds while mgilson’s takes about 2.8 seconds.
from timeit import timeit
data = "lst = ['one', 'two', 'three'] * 1000; max_length = 12345678"
print(timeit('q, r = divmod(max_length, len(lst)); q * lst + lst[:r]',
data,
number=10))
print(timeit('list(islice(cycle(lst), max_length))',
data + '; from itertools import cycle, islice',
number=10))