Python List is a collection of ordered values and can hold any type of value. It is a container where we can insert, delete or modify the existing values as the list is mutable in Python. Unlike sets, the list can hold the same value multiple times and considers each as a separate item. In this tutorial, we will read how to initialize a list in Python.
- Introduction to Python Initialize List
- Initialize using the square brackets in Python
- Using the list() function to initialize the list
- Using comprehensions to initialize the list
- Initialize using * operator
- Closing thoughts
In Python, a user can perform a single task using different ways. Similarly, in order to initialize a list in Python, there is more than one way to do that and we will discuss a few in this tutorial.
If you want to create an empty list with no values in Python, the square bracket is the way to initialize the list with empty values. You just have to declare a non-value list by specifying a set of square brackets without item values.
list =  print (list) list = [1,2,3,4] print (list)
It is another way to create an empty list without values in Python as the list() function creates the list from the iterable object.
An iterable may be a container, a sequence that supports iteration, or an iterator object. If no parameter is specified, then a new empty list is created.
list_1 = list() print (list_1) res =  print (data == res)
Since the square bracket approach is more instructive and concise, it is preferred over the list() function approach.
In order to initialize the list with default values, we can use the comprehension method. 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 you want to put on the list. Since the user is initializing the list with zeros, the expression will just be 0.
List comprehension is an easy way to define a list based on an iterator because it is elegant, simple, and widely recognized.
list = [i for i in range(5)] print (list)
This method is way faster than using for and while loops to initialize lists in Python.
Another approach to initialize the list with multiple values is the * operator. The syntax is [object]*n where n is the no of elements in the array.
This method allows us to create a list with a specific number of predefined values.
list = *10 print (list)
This is the fastest method among all the methods discussed to initialize the list in Python.
The only drawback of using the * operator to initialize a list in Python is while declaring 2d arrays as it creates shallow lists i.e only one list object would be created and all the indices would refer to this object which can be inconvenient. That is why in the case of 2d arrays, comprehensions are the better way to initialize a list.
Initializing the list is one of the basic things to know while working with lists. We have discussed square brackets and the list() function to create empty lists and comprehensions and the * operator to create a list with multiple values. One can learn about other Python concepts here.