Skip to content
Home » Create Empty List in Python

Create Empty List in Python

In this tutorial let’s learn about How to Create Empty List in Python. The list in Python is similar to an array, which is a data structure that is an ordered collection of specified elements enclosed by square brackets. In this article, we’ll look at what an empty list is and how to declare one in Python. An empty list is defined in Python as a list that has no elements or items.

Declare Empty List in Python

An empty list in python can be declared using two ways.

  • square brackets []
  • list()

By simply defining square brackets [] with no elements within the assignment statement’s brackets, an empty list is created. It can also be created using the built-in method list() method or constructor. Let’s go over these two approaches in depth with examples that show how to make an empty list in Python.

Create Empty List in Python using [ ] square brackets

Using square brackets [], an empty list can be created by inserting element sequences inside the square brackets. This is accomplished by simply assigning the list variable with square brackets [].

Code Example :

# empty list
l = []		

print("List:", l)
print("Type of list:", type(l))
print("Size of list:", len(l))	

Output :

List: []
Type of list: <class ‘list’>
Size of list: 0

Create Empty List in Python using list( ) constructor

To create a list in Python we use the list() constructor or built-in function. This constructor can also be used to make an empty list. Let us now look at an example and syntax for using the list() constructor to create an empty list.

Syntax: list([iterable])

Parameters:
iterable: This is an optional argument which can be either list, set, tuples, dictionary, etc.

Return Type:
It returns an empty list if no parameters are passed.
If a parameter is passed then it returns a list of those elements that is passed as a iterable.

Code Example :

# create empty list using list()
l = list()

print("List:", l)
print("Type of list:", type(l))
print("Size of list:", len(l))	

Output :

List: []
Type of list: <class ‘list’>
Size of list: 0

Which is faster: [ ] or list()

list() is slower because it must look for the function’s name, call it, and then create the list object in memory. In contrast, [] is a shortcut that eliminates the need for so many intermediate steps to generate the list in memory.

This time difference will have little effect on the performance of your software, but it’s useful to know which is more efficient and how they work behind the scenes.

Adding elements to the empty List

The element is appended to the end of the list using the list append() function. The append() method takes one input, which is an element to be added to the list. Likewise you can add or append an element to the empty list in python.

#  empty list 
l = []

for i in range(1,5):
    l.append(i)
    
print("List:", l)
print("Type of list:", type(l))
print("Size of list:", len(l))	

Output:

List: [1, 2, 3, 4]
Type of list: <class ‘list’>
Size of list: 4

Conclusion

An empty list can be created with an empty pair of square brackets [] or list() constructor that creates an empty list when no arguments are passed.
We saw that there are two ways to create an empty list with examples: using square brackets [] and using the list() function. We also compared the two ways to see which one is faster and why.

Because square brackets [] are faster and more clear, they are commonly used in Python to create empty lists.

Similar Posts:

Python List Length | How to Find the Length of List
How To Convert Python List To CSV File | Python