• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Lists / Repeat Elements of a List in Python

Repeat Elements of a List in Python

Author: Aditya Raj
Last Updated: June 17, 2023

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.

Table of Contents
  1. How To Repeat Elements of a List in Python
  2. Repeat Elements Of a List Using the append() Method
  3. Repeat Elements Of a List Using the extend() Method
  4. Using the * Operator
  5. Conclusion

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!

Related

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.

Enroll Now

Filed Under: Lists Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us