Create List of single item repeated N times in Python

In this article, we will learn different ways to create a list of single item repeated N times in Python.

Table Of Contents

Method 1: Using Astrik

We can create a list object with one element only, and then multiply that list object with N. It will repeat the single element of list N times, and returns a list of N repeated elements. Let’s see an example,

N = 7

# Create a List of N repeated numbers i.e. 
# seven times 22 in this case
listOfNumbers = [22] * N

print(listOfNumbers)

Output:

[22, 22, 22, 22, 22, 22, 22]

It created a list of 7 repeated elements i.e. number 22 repeated seven times.

Advertisements

Method 2: Using List comprehension

We can iterate over a range of numbers i.e. from 0 to N. In each iteration, add same element into the list. In the end, list comprehension will give us a list of N repeated elements. Let’s see an example, where we will create a list of 7 elements.

N = 7

# Create a List of N repeated numbers i.e. 
# seven times 22 in this case
listOfNumbers = [22 for i in range(N)]

print(listOfNumbers)

Output:

[22, 22, 22, 22, 22, 22, 22]

It created a list of 7 repeated elements i.e. number 22 repeated seven times.

Method 3: Using itertools

The itertools module, provides a function repeat(), almost exactly for this purpose. The function accepts an element, and a repeation count as arguments. It yields the object for the specified number of times. We can use this to create a list of N repeated items. For that, we need to pass th element and repeation count N as arguments in the itertools.repeat() function. Then store the returned values to a list. Let’s see an example,

import itertools

N = 7

# Create a List of N repeated numbers i.e. 
# seven times 22 in this case
listOfNumbers = list(itertools.repeat(22, N))

print(listOfNumbers)

Output:

[22, 22, 22, 22, 22, 22, 22]

It created a list of 7 repeated elements i.e. number 22 repeated seven times.

Summary

We learned how to create a list of N repeated items in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top