Python : How to create a list and initialize with same values

In this article we will discuss how to create a list and initialise with same values.

Creating a list of same values by [] and multiply

Suppose we want to create a list of strings, which contains 20 same strings i.e. ‘Hi’

['Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi']

Let’s see how to do that i.e.
''' create a list by [] and multiply by repeat count '''
listOfStrings1 = ['Hi'] * 20

[‘Hi’] will create a list with single value, then we can multiply this list by 20. It will repeat the contents of list 20 times.
So, lists content will be now i.e.
['Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi']

Creating a list of same values by List Comprehension with range()

This is an another way to create a list of same value using range() i.e.

''' 
    Use List Comprehension with range() to initialize a list by 20 elements 0
    It will iterate over the tange from 0 to 20 and for
    each entry, it will add 'Hi' to the list and in the end 
    returns the list to listOfNums
'''
listOfStrings2 = ['Hi' for i in range(20)]

In this list comprehension, for loop will iterate over the range object 20 times and in each iteration it will add ‘Hi’ in the list.
So, list will coatain 20 ‘Hi’ elements i.e.
['Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi']

Complete example is as follows,
def main():
    
    ''' create a list by [] and multiply by repeat count '''
    listOfStrings1 = ['Hi'] * 20

    print(listOfStrings1)
    

    ''' 
    Use List Comprehension with range() to initialize a list by 20 elements 0
    It will iterate over the tange from 0 to 20 and for
    each entry, it will add 'Hi' to the list and in the end 
    returns the list to listOfNums
    '''
    listOfStrings2 = ['Hi' for i in range(20)]
    
    print(listOfStrings2)
    
    
if __name__ == '__main__':
    main()

Output:
['Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi']
['Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi', 'Hi']

 

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

1 thought on “Python : How to create a list and initialize with same values”

  1. Christopher BARRETT

    This is an absolutely brilliant website!

    I have been away from IT for over 8 years, and I am making a return to this industry due to the large shortage of IT professionals. I have therefore been learning new skills and relearning old skills.

    I have been covering both Python and C++ in detail, along with MySQL, PHP, SQL, PL/SQL and testing, the last 3 I am very familiar with.

    This site gives me all sorts of very useful information that I need and how to solve problems the best way possible.

Leave a Comment

Your email address will not be published.

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

Scroll to Top