There are two ways to create a new empty list in Python, as follows:
l = []
or
l = list()
list() is slower than [] , because:
1. There is a symbolic search;
2. There is a function call;
3. Then, it must check whether the iterable parameter is passed.
But in most cases, the speed difference won't have any real impact.
Here's a test of which piece of code is faster:
% python -mtimeit "l=[]" 10000000 loops, best of 3: 0.0711 usec per loop % python -mtimeit "l=list()" 1000000 loops, best of 3: 0.297 usec per loop
I prefer [] because readability is very subjective.
Many python training videos, all on the python learning network, welcome to learn online!
The above is the detailed content of How to create an empty list in python. For more information, please follow other related articles on the PHP Chinese website!