To create a list with same value repeated N times in Python, there are three solutions we have effectively tested. Follow the article to understand better.
Create a list with same value repeated N times in Python
Use the ‘*’ operator with the number of times to repeat
The easiest way is to use the operator ‘*’ combined with a specified number of iterations to output the string you want.
Example:
myName = ['John'] * 5 print(myName)
Output:
['John', 'John', 'John', 'John', 'John']
Define ‘my_name’ using a list, 5 being the number of iterations combined with the ‘*’operator to return a list with ‘John’ iterating five times.
Use a for range loop
Python for range for loop aka for i in range python is a combination of for loop and range() function in python, where the object with multiple elements of the for loop is specified by range() function. For i in range, python is used to repeat a specific number of times.
Syntax:
for i in range ( number of repetitions )
Example:
for i in range(5): print(i + 1,'.John')
Output:
1 .John
2 .John
3 .John
4 .John
5 .John
Because in the Python for loop, we will get each element in the object with many specified elements to process, the result, when combined with the range(5) function above, there will be five iterations.
Use ‘itertools’
‘Itertools‘ is a module in Python that provides functions that operate on iterators to create complex loops.
Specifically, here I will refer to the itertools.repeat() function. The itertools.repeat() function is an infinite iterator. You give data and specify how many times the data should be repeated into the function. The itertools.repeat() function does not create a memory for every variable. Instead, it creates a variable and iterates over it.
Syntax:
itertools.repeat(value,num)
Parameters:
- value: the value you need to loop
- num: number of iterations
Example:
import itertools print(list(itertools.repeat('John', 5)))
Output:
['John', 'John', 'John', 'John', 'John']
Note: The itertools.repeat() function creates an iterator, not a list, so use the ‘list’ function to convert the iterator.
One of the features of the ‘itertool.repeat’ function is that it defines computations until needed. So it is suitable for creating an iterator or an extensive list.
Summary
There are many methods to create a list with same value repeated N times in Python. Here are some solutions to resolve it. Find the best way that works for you. We hope this tutorial is helpful to you. Thanks!
Maybe you are interested:
- Join a list of lists in Python
- Multiply each element in a list by a number in Python
- Split a string into a list of characters in python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java