Python | What`s faster to initialize lists?



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
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 = [0] * 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.

# import a time unit for timing

import time

 
# initialize lists to save time

forLoopTime = []

whileLoopTime = []

listComprehensionTime = []

starOperatorTime = []

  
# repeat the process 500 times
# and calculate the average time.

for k in range ( 500 ): 

 

# start time

start = time.time ()

  # declare an empty list

a = []

  # run loop for 10,000 times

  for i in range ( 10000 ):

a.append ( 0 )

# stop time

stop = time.time ()

forLoopTime.append (stop - start)

 

# start time

start = time.time ()

  # declare an empty list

  a = []

  i = 0

# run the loop 10,000 times

while (i & lt; 10000 ) :

a.append ( 0 )

i + 1

stop = time.time ()

whileLoopTime.append (stop - start)

 

start = time.time ()

# list comprehension to initialize the list

a = [ 0 for i in range ( 10000 )] 

  stop = time.time ()

listComprehensionTime.append (stop - start)

 

 

  start = time.time ()

# using the * operator

a = [ 0 ] * 10000  

  stop = time.time ()

starOperatorTime.append (stop - start)

  

  

 

print ( "Average time taken by for loop:" + str ( sum (forLoopTime) / 100 ))

print ( "Average time taken by while loop:" + str ( sum (whileLoopTime) / 100 ))

print ( "Average time taken by list comprehensions:" + str ( sum (listComprehensionTime) / 100 ))

print ( "Average time taken by * operator:" + str ( sum (starOperatorTime) / 100 )) 

Exit

 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.

  • As you can see, for and while loops take almost the same time as a for loop with a small margin.
  • Comprehension lists perform much better than for and while loops, with the former about 3-5 times faster. Another example of this difference can be seen when we try to create a list of numbers from 1 to 1000. Using a list comprehension is much better than using append ().
     a = [i for i in range (1, 1001)]  
  • Using the * operator is much faster than the rest of the methods and this is how you should initialize lists.

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 = [[0] * 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)]