How to create an empty list in python

In this post, we will see how to create an empty list in python.

There are two ways to create an empty list in Python.

  • Using [ ]
  • Using list() function.

Let’s see this with the help of example.

Output:

List 1: [1, 2] List 2: [1, 2]

Which is faster: [ ] or list()

list() is inherently slower than [], because of

  • symbol lookup (no way for python to know in advance if you did not just redefine list to be something else!)
  • function invocation
  • then it has to check if there was iterable argument passed (so it can create list with elements from it)

That’s all about creating empty list in python.

Was this post helpful?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *