• 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: February 8, 2022

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 the existing elements of a list to 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 the 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 in 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).

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 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.

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 bitly 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 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 Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Select Specific Columns in Pandas Dataframe
  • Convert Epoch to Datetime in Python
  • Iterate Rows in a Pandas Dataframe
  • Split String Into Characters in Python
  • Pandas Map Function to Series in Python

Copyright © 2012–2023 ยท PythonForBeginners.com

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