How to Initialize a List in Python?
Any Python object can be contained in a group of ordered values in a Python List. Because the list is a mutable data structure in Python, we can add, remove, or alter the existing values in this container. In contrast to sets, the list allows numerous instances of the same value and treats each as a different item. In this tutorial, we will learn how to initialize a list object in Python.
Initialize the Lists Using the Square Brackets
Using a square bracket is one way to initialize a list with no values if we want to construct an empty list in Python with no values. To initialize a list, we only need to specify a pair of square brackets with or without item values.
An empty list:  A non-Empty list: [1, 3, 5, 7]
Using the Built-in list() Function to Initialize a List
Python's list() function constructs the list, an iterable object. Therefore, this is another way to create an empty Python list without any data in this coding language.
An iterator object, a sequence that enables iteration, or a container can all be iterables. A new empty list is constructed if no input is given.
An empty list:  A non-empty list: [1, 2, 3]
The square bracket method is favored over the built-in list() function because it is clearer and more illustrative.
Using List Comprehensions to Initialize a List
We can employ the list comprehension approach to set the list's default parameters. It comprises an expression enclosed in square brackets, a for statement, and an optional if statement that may or may not follow. Any item we wish to add to the list can be written as an expression. The expression would be 0 if the user initialized the list with zeros.
List comprehension is an elegant, straightforward, and well-known approach to constructing a list premised on an iterator.
The list was created using list comprehension: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
This technique initializes lists much faster than Python's for and while loops.
Initialize a Python List Using the * Operator
Another way to initialize a list in Python is using the * operator. It creates a list with multiple values. The syntax of using this operator is [element] * n. Here n is the number of times we want to repeat the element in the list.
This method helps when we wish to initialize a list of predefined lengths.
[5, 5, 5, 5, 5, 5, 5, 5, 5]
This method is very efficient and the fastest way to create a list. We will compare the time taken by the methods later in this tutorial.
The only con of using this operator to initialize a Python list is when we have to create a 2D list, as this method will only create a shallow list, i.e., it will create a single list object, and all the indices will refer to this object which will be very inconvenient. This is why we use list comprehension when we have to create 2D lists.
Using a for Loop and append()
We will create an empty list and run a for loop to add items using the append() function of the list.
Using a While Loop to Initialize a List
We can use a while loop just like we used for loop to initialize a list.
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Let us now see how long each of the described approaches will take. We will initialize a list of 100000 elements 1000 times. We will calculate the average time each method takes to perform this task.
The average execution time of for loop is: 0.01166958212852478 The average execution time of the while loop is: 0.01916465663909912 The average execution time of list comprehension is: 0.005084690093994141 The average execution time was taken of * operator: 0.00028331947326660156
We can see that for and while loops take almost the same execution time. However, for loop is a little better than the while loop.
List comprehension shows much better performance than the for and while loops. It is 2-3 times faster than the loops. Thus, list comprehension is much more efficient than the append() function of the lists.
The * operator has shown the best performance out of all the four methods.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week