Home  >  Article  >  Backend Development  >  How to create an empty list in python

How to create an empty list in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2020-01-06 10:18:4914576browse

How to create an empty list in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn