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.
1 2 3 4 5 6 7 8 9 10 11 12 |
list1=[] list1.append(1) list1.append(2) list2=list() list2.append(1) list2.append(2) print("List 1:",list1) print("List 2:",list2) |
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?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.
thanks for this simple example