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’

Let’s see how to do that i.e.

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

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.

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.

Complete example is as follows,



Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.