Repeat List N Times in Python

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.

Output:

[2, 4, 6, 11, 2, 4, 6, 11, 2, 4, 6, 11]

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.

Output:

[2, 2, 2, 4, 4, 4, 6, 6, 6, 11, 11, 11]

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.

Output:

[2, 2, 2, 4, 4, 4, 6, 6, 6, 11, 11, 11]

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.

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.

Output:

[2, 2, 2, 4, 4, 4, 6, 6, 6, 11, 11, 11]

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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 16 December

    Convert List to Comma Separated String in Python

    Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63d294887d77a701471841/] [crayon-63d294887d781607722608/] First, we created a […]

  • 27 November

    Remove Punctuation from List in Python

    Table of ContentsUse for loop with replace() MethodUse Nested List ComprehensionUse str.translate() Function Use for loop with replace() Method To remove punctuation from list in Python: Use the for loop to iterate over each word (element) of given list. Use a nested loop to iterate over each character in the word. Use the if statement […]

  • 04 October

    Get Every Other Element in List in Python

    Table of ContentsGet Every Other Element in List in PythonUsing List SlicingUsing the list.append() function along with the range() functionUsing list comprehension and the range() function with conditionalsUsing list comprehension and the range() function with stepUsing the enumerate() function along with list comprehensionUsing a lambda functionConclusion. A list is one of the more versatile data […]

  • 23 September

    Add Tuple to List in Python

    Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.Conclusion In this post, we will […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusion In this post, we will see how to compare list elements with each other in Python. Compare all […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.