Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create an empty list in Python?
In Python, a list is one of the built-in data types. A Python list is a sequence of items separated by commas and enclosed in square brackets [ ]. The items in a Python list need not be of the same data type.
In this article, we will discuss different ways to create an empty list in Python.
Using Square Brackets
This is the simplest and most common way to create an empty list using square brackets []. An empty list means the list has no elements at the time of creation, but we can add items to it later when needed ?
my_list = []
print("Empty list:", my_list)
print("List length:", len(my_list))
The output of the above code is ?
Empty list: [] List length: 0
Using list() Constructor
In Python, we have another way to create an empty list using the built-in list() constructor. This constructor is commonly used for typecasting (converting one data type to another), but it can also initialize an empty list when called without arguments ?
my_list = list()
print("Empty list using list() constructor:", my_list)
print("List type:", type(my_list))
The output of the above code is ?
Empty list using list() constructor: [] List type: <class 'list'>
Using Multiplication with Zero
In Python, we can use the multiplication operator * to repeat elements in a list. When a list is multiplied by 0, it effectively removes all elements, resulting in an empty list. Even though the original list contains elements, multiplying by zero results in an empty list ?
my_list = ['h', 6] * 0
print("Empty list using multiplication operator:", my_list)
# Alternative example
another_list = [1, 2, 3] * 0
print("Another example:", another_list)
The output of the above code is ?
Empty list using multiplication operator: [] Another example: []
Comparison of Methods
| Method | Syntax | Performance | Best Use Case |
|---|---|---|---|
| Square brackets | [] |
Fastest | General purpose |
| list() constructor | list() |
Slightly slower | When clarity is important |
| Multiplication | [items] * 0 |
Slowest | Educational/special cases |
Adding Elements to Empty Lists
Once you have created an empty list, you can add elements using methods like append(), extend(), or insert() ?
# Create empty list
fruits = []
print("Initial list:", fruits)
# Add elements
fruits.append("apple")
fruits.append("banana")
print("After adding elements:", fruits)
The output of the above code is ?
Initial list: [] After adding elements: ['apple', 'banana']
Conclusion
The most common and efficient way to create an empty list in Python is using square brackets []. The list() constructor provides better readability, while multiplication with zero is primarily educational. All methods create identical empty list objects that can be populated with elements later.
