Lists in Python are one of the most used data structures. We have already discussed various operations on lists like counting the frequency of elements in a list or reversing a list. In this article, we will look at different ways to repeat elements of a list in Python.
How To Repeat Elements of a List in Python
To repeat the elements in a list in Python, we insert all the existing elements of a list into the same list. In this way, the elements in a list get repeated.
For instance, If we have a list myList=[1,2,3,4,5]
and we have to repeat the elements of the list two times, the output list will become [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
.
We can use the append() method, the extend() method, or the * operator to repeat elements of a list in Python. We will discuss all these methods in detail in the following sections.
Repeat Elements Of a List Using the append() Method
The append() method is used to append elements to the last of a list. When invoked on a list, the append() method takes an element and adds it to the list as shown below.
myList = [1, 2, 3, 4, 5]
print("The given list is:", myList)
number = 10
print("The input number to append to list is:", number)
myList.append(number)
print("The output list is:", myList)
Output:
The given list is: [1, 2, 3, 4, 5]
The input number to append to list is: 10
The output list is: [1, 2, 3, 4, 5, 10]
To repeat the elements of the list N times, we will first copy the existing list into a temporary list. After that, we will add the elements of the temporary list to the original list N times using a for loop, range() method, and append() method. After execution of the for loop, the elements in the list will be repeated n times as shown in the following example.
myList = [1, 2, 3, 4, 5]
print("The given list is:", myList)
tempList = list(myList)
count = 2
print("Number of Times to repeat the elements:",count)
for i in range(count):
for element in tempList:
myList.append(element)
print("The output list is:", myList)
Output:
The given list is: [1, 2, 3, 4, 5]
Number of Times to repeat the elements: 2
The output list is: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Repeat Elements Of a List Using the extend() Method
Instead of adding each element one by one using the append() method, we can use the extend() method to add entire elements of the list at once to repeat them. The extend() method, when invoked on a list, accepts a list as input arguments and adds the input list to the existing list as shown below.
myList = [1, 2, 3, 4, 5]
print("The given list is:", myList)
newList = [6, 7]
print("The input list to append elements from is:", newList)
myList.extend(newList)
print("The output list is:", myList)
Output:
The given list is: [1, 2, 3, 4, 5]
The input list to append elements from is: [6, 7]
The output list is: [1, 2, 3, 4, 5, 6, 7]
To repeat elements of a list using the extend() method, we will copy the elements of the existing list into a temporary list. After that, we will use the extend() method with a for loop to repeat the elements of the list as follows.
myList = [1, 2, 3, 4, 5]
print("The given list is:", myList)
tempList = list(myList)
count = 2
print("Number of Times to repeat the elements:",count)
for i in range(count):
myList.extend(tempList)
print("The output list is:", myList)
Output:
The given list is: [1, 2, 3, 4, 5]
Number of Times to repeat the elements: 2
The output list is: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Using the * Operator
The * operator can also be used to repeat elements of a list. When we multiply a list with any number using the * operator, it repeats the elements of the given list. Here, we just have to keep in mind that to repeat the elements n times, we will have to multiply the list by (n+1) as shown below.
myList = [1, 2, 3, 4, 5]
print("The given list is:", myList)
count = 2
print("Number of Times to repeat the elements:", count)
myList = myList * 3
print("The output list is:", myList)
Output:
The given list is: [1, 2, 3, 4, 5]
Number of Times to repeat the elements: 2
The output list is: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Conclusion
In this article, we have discussed three ways to repeat elements of a list in Python. In all these above approaches, the approach using the * operator is the most efficient and easy to implement. So, I would suggest you use this method to repeat elements of a list in Python. To know more about lists in Python, you can read this article on list comprehension in Python. You might also like this article on the random module in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.