Table of Contents
This tutorial will demonstrate how to repeat list n times in Python.
Using the *
operator
To repeat list n times in Python, use the *
operator. Star operator(*
) is used to multiply list by number e.g. lst*3
and this will repeat list 3 times.
See the code below.
1 2 3 4 5 |
lst = [2, 4, 6, 11] lst_new = lst * 3 print(lst_new) |
Output:
In the above example, we demonstrate the use of the *
operator to repeat list n times in Python. This will repeat the elements of the list the required number of times.
Using the numpy.repeat()
function
To repeat list n times in Python:
- Use the
numpy.repeat()
function to repeat every element n times in Python.
See the code below.
1 2 3 4 5 6 |
import numpy as np lst = [2, 4, 6, 11] lst_new = list(np.repeat(lst,3)) print(lst_new) |
Output:
The numpy.repeat()
function repeats the elements of the array. It will repeat the elements individually the required number of times and return a numpy
array.
We can convert this array back to a list using the list()
function.
Using the list comprehension technique
To repeat list n times in Python:
- Use the
for
loop to iterate over the list elements and create a new list using list comprehension. - Use another
for
loop to iterate over every element and repeat it the required number of times.
See the code below.
1 2 3 4 5 |
lst = [2, 4, 6, 11] lst_new = [a for a in lst for i in range(3)] print(lst_new) |
Output:
List comprehension is an elegant way to create lists using the for
loop in a single line of code.
To repeat list n times in Python, we use two loops in list comprehension.
The first loop iterates over every element and the second loop will repeat it the required number of times.
Further reading:
Using the itertools.repeat()
function
To repeat list n times in Python:
- Use the
itertools.repeat()
function to repeat elements from an iterable. - Use the
itertools.from_iterable()
function to return a flattened iterable from the input.
See the code below.
1 2 3 4 5 6 |
import itertools lst = [2, 4, 6, 11] lst_new = list(itertools.chain.from_iterable(itertools.repeat(i, 3) for i in lst)) print(lst_new) |
Output:
The itertools
library is used to work with complex iterables efficiently in Python.
We can use the itertools.repeat()
method to repeat the elements of a list and flatten it using the from_iterable()
function.
Conclusion
To conclude, we discussed several methods to repeat list n times in Python.
In the first method, we used the *
operator to repeat a list the required number of times. In the following method, the numpy.repeat()
function was used to repeat individual elements of the list.
We demonstrated the use of the list comprehension technique to create a new list by using two successive for
loops. The first loop will create a new list of the elements from the original list and the second loop will repeat these elements.
In the final method, we discussed the use of the itertools
library that can be used to deal with complex iterables in Python. We used the itertools.repeat()
and itertools.chain.from_iterable()
functions in this method.
That’s all about how to repeat List N time in Python.