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.
list1= list1.append(1) list1.append(2) list2=list() list2.append(1) list2.append(2) print("List 1:",list1) print("List 2:",list2)
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.