How to create an empty list in python

爱喝马黛茶的安东尼
Release: 2020-01-06 10:18:49
Original
14633 people have browsed it

How to create an empty list in python

There are two ways to create a new empty list in Python, as follows:

l = []
Copy after login

or

l = list()
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template