Python is a very flexible language where a single task can be performed in a number of ways, for example initializing lists can be performed in many ways. However there are subtle differences in these seemingly similar methods. Python which is popular for its simplicity and readability is equally infamous for being slow compared to C++ or Java. The ‘for’ loop is especially known to be slow whereas methods like map() and filter() known to be faster because they are written in C. Knowing the better and faster way to initialize lists might give you a slight edge in competitive programming.
The following are some of the ways to initialize lists(we create lists of size 1000 and initialize with zeros) in Python.
Using a for loop and append()
We create an empty an list and run a for loop for n times using the append() method to add elements 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 while loop instead.
arr =  i = 0 while(i<1000): arr.append(0)
Using list comprehensions
It consists of square brackets containing an expression followed by a for clause and further 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 just be 0.
arr = [0 for i in range(1000)]
Using the * operator
The * operator can be used as [object]*n where n is the no of elements in the array.
arr = *1000
Lets look at the time taken by each of them. We will calculate the average time taken by each of these methods to initialize an array of 10000 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
Note: The times will vary based on the platform where this code is executed. These times are just for studying the relative performance of these methods of initializing.
- As it can be seen, for and while loops take almost the same time with for loop having a slight edge.
- List comprehensions perform much better than for and while loops with the former being around 3-5 times faster. Another example of this difference can be seen when we try to create a list of numbers from 1-1000. Using list comprehensions is much better than using append().
a = [i for i in range(1, 1001)]
- Using the * operator is way faster than the remaining methods and this is the way you should be initializing lists
However one drawback of using the * operator is while declaring 2d arrays. Using this operator would create shallow lists i.e only one list object would be created and all the indices would refer to this object. This might create unwanted complications. Hence using list comprehensions 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)]
- Python | Making program run faster
- Initialize Matrix in Python
- Python | Initialize dictionary with common value
- Python | Initialize common value to keys
- Python | Initialize dictionary with None values
- Python | Initialize list with empty dictionaries
- Python | Initialize a dictionary with only keys from a list
- Python | Ways to initialize list with alphabets
- Python | Initialize dictionary with multiple keys
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
- Python | Union of two or more Lists
- Python | Dividing two lists
- Python | Intersection of two lists
- Python | Merge key value lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.