Open In App

Declare an empty List in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Lists are just like arrays, declared in other languages. Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain data types like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation. 

However, Have you ever wondered about how to declare an empty list in Python? Let us see how we can achieve it in Python.

Declare an Empty List in Python

Declaring an empty list can be achieved in two ways i.e. either by using square brackets[] or using the list() constructor. 

Create an Empty List in Python using Square Brackets [] 

We can create an empty list in Python by just placing the sequence inside the square brackets[]. To declare an empty list just assign a variable with square brackets.

Example:

In this example, we will create a Python empty list using square brackets, and then print the list and check its type and length.

Python3




# Python program to declare
# empty list
 
# list is declared
a = []        
 
print("Values of a:", a)
print("Type of a:", type(a))
print("Size of a:", len(a))


Output:

Values of a: []
Type of a: <class 'list'>
Size of a: 0

Create an Empty List in Python using the list() Constructor 

The list() constructor is used to create a list in Python.

Syntax: list([iterable]) 

Parameters: 

  • iterable: This is an optional argument that can be a sequence(string, tuple) or collection(dictionary, set) or an iterator object. 

Return Type: 

  • Returns an empty list if no parameters are passed.
  • If a parameter is passed then it returns a list of elements in the iterable.

Example:

In this example, we will create a Python empty list using the list() constructor and then print the list along with its type and length.

Python3




# Python program to create
# empty list
 
# list is declared
a = list() 
 
print("Values of a:", a)
print("Type of a:", type(a))
print("Size of a:", len(a))


Output:

Values of a: []
Type of a: <class 'list'>
Size of a: 0

Python Append to Empty List

In Python, we can create an empty list and then later append items to it, using the Python append() function.

Example:

In this example, we will first create an empty list in Python using the square brackets and then appended the items to it.

Python3




# create an empty list
a = []
 
# append items to it
a.append(10)
a.append(20)
a.append(30)
 
print("Values of a:", a)
print("Type of a:", type(a))
print("Size of a:", len(a))


Output:

Values of a: [10, 20, 30]
Type of a: <class 'list'>
Size of a: 3


Last Updated : 05 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads