Python create empty list | Example code

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:

Python create empty list

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

  1. there is symbol lookup (no way for python to know in advance if you did not just redefine list to be something else!),
  2. there is function invocation,
  3. 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.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.