
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create an Empty List in Python
In Python, list is one of the built-in data types. A Python list is a sequence of items separated by commas, 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 one of the simplest way to create an empty list to 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)
Following is the output of the above code -
Empty list: []
Using list() Constructor
In Python, we have another way to create an empty list using the built-in list() constructor. This constructor is used in typecasting, which means converting one data type to another data type (here, converting into a list). It can also be used to initialize an empty list.
my_list=list() print("Empty list using list() constructor:",my_list)
Following is the output of the above code ?
Empty list using list() constructor: []
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.
In following example, even though the original list contains elements 'h'
and 6
, when multiplied with zero, it results in an empty list -
my_list=['h',6]*0 print("Empty list using multiplication operator:",my_list)
Following is the output of the above code -
Empty list using multiplication operator: []