A simple way to create an empty list in Python is by just placing the sequence inside the square brackets[ ]. Don’t add any value just assign a variable with square brackets.
A declaration of an empty list
List = []
Python creates an empty list Example
Simple Python program to declare empty list.
list1 = []
print("Values of a:", list1)
print("Type of a:", type(list1))
print("Size of a:", len(list1))
Output:
Another way Using list() constructor
The list() constructor is used to create an empty list in Python.
a = list()
print("Values of a:", a)
Output: Values of a: [ ]
Q: What is the best way to create a new empty list in Python?
Answer:
list()
is inherently slower than []
, because
- there is symbol lookup (no way for python to know in advance if you did not just redefine list to be something else!),
- there is function invocation,
- then it has to check if there was iterable argument passed (so it can create list with elements from it) ps. none in our case but there is “if” check
Source: stackoverflow.com/
Do comment if you have any doubts and suggestions on this Python list topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.