Declaring an empty list in Python creates a list with no elements, ready to store data dynamically. We can initialize it using [] or list() and later add elements as needed.
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.
a = []
print(a)
print(type(a))
print(len(a))
Output
[] <class 'list'> 0
Explanation:
- a = []: Initializes an empty list and assigns it to variable a.
- print(a): Prints the empty list [].
- print(type(a)): Prints the type of a, which is <class 'list'>, confirming that a is a list.
- print(len(a)): Prints the length of a, which is 0, since the list is empty.
Using the list() Constructor
list() constructor is used to create a list in Python. It returns an empty list if no parameters are passed.
a = list()
print(a)
print(type(a))
print(len(a))
Output
[] <class 'list'> 0
Explanation:
- a = list(): Initializes an empty list using the list() constructor and assigns it to variable a.
- print(a): Prints the empty list [].
- print(type(a)): Prints the type of a, which is <class 'list'>, confirming that a is a list.
- print(len(a)): Prints the length of a, which is 0, since the list is empty.
Related Articles: