DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

Python Initialize List - How to do it?

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.

Table of contents

Introduction to Python Initialize List

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.

Initialize lists using the square brackets in Python

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.

Input:

list = []
print (list)

list = [1,2,3,4]
print (list)
Enter fullscreen mode Exit fullscreen mode

Output:

[]
[1,2,3,4]
Enter fullscreen mode Exit fullscreen mode

Using the list() function to initialize the list in Python

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.

Input:

list_1 = list()
print (list_1)

res = []
print (data == res)
Enter fullscreen mode Exit fullscreen mode

Output:

[]
TRUE
Enter fullscreen mode Exit fullscreen mode

Since the square bracket approach is more instructive and concise, it is preferred over the list() function approach.

Using comprehensions to initialize the list in Python

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.

Input:

list = [i for i in range(5)]
print (list)
Enter fullscreen mode Exit fullscreen mode

Output:

[0,1,2,3,4]
Enter fullscreen mode Exit fullscreen mode

This method is way faster than using for and while loops to initialize lists in Python.

Initialize lists using the * operator 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.

Input:

list = [5]*10
print (list)
Enter fullscreen mode Exit fullscreen mode

Output:

[5,5,5,5,5,5,5,5,5,5]
Enter fullscreen mode Exit fullscreen mode

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.

Closing thoughts

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.

Discussion (0)