Below are some ways to initialize lists (we create 1000-size lists and initialize to zeros) in Python.
Using a for and append () loop
We create an empty list and run the for loop n times, using the append () method to add items to the list.
arr =  for i in range (1000): arr.append (0)
Using a while loop with a counter variable
This is similar to the above method. However, we use a while loop instead.
arr =  i = 0 while (i & lt; 1000): arr.append (0)
Using list views strong>
It consists of square brackets containing an expression followed by a for clause followed by an optional if clause. The expression can be any type of object that we want to put in the list. Since we are initializing the list with zeros, our expression will be just 0.
arr = [0 for i in range (1000)]
Using the * operator
Operator * can be used like [object] * n, where n — this is the number of elements in the array.
arr =  * 1000
Let`s take a look at the time taken by each of them. We will calculate the average time it takes each of these methods to initialize an array of 10,000 elements over 500 times.
Average time taken by for loop: 0.012432687282562256 Average time taken by while loop: 0.017907898426055908 Average time taken by list comprehensions: 0.0034629487991333007 Average time taken by * operator: 0.0001951146125793457
The timing will depend on the platform this code is running on. This time is just to explore the relative effectiveness of these initialization methods.
a = [i for i in range (1, 1001)] pre>
However, one of the disadvantages of using the * operator is declaring 2d arrays ... Using this operator will create shallow lists, i.e. only one list object will be created and all indexes will refer to that object. This can create unwanted complications. Therefore, using list comprehension is a safer way to create 2d lists.
Using * operator would create shallow lists arr = [ * no_of_cols] * no_of_rows Using list comprehensions is better for 2d arrays arr = [[ 0 for i in range (no_of_cols)] for j in range (no_of_rows)]