List vs Array

What Is the Difference Between list() and [] in Python?

list() and [] both make a new empty Python list. Learn how they differ when converting iterables and in speed, and how C++ std::vector compares.

On this page
  1. The short answer
  2. Converting other values
  3. Speed
  4. Shadowing the name list
  5. Comparing with C++
  6. Summary

The short answer

Both [] and list() create a new, empty list. They differ in how they get there. [] is a list literal, which Python builds directly. list() calls the built-in list type to make one.

a = []
b = list()
print(a == b, type(a) is type(b))  # True True

Both results are mutable, so you can append to either one.

Converting other values

list() takes any iterable and builds a list from its items. [] takes the items you write between the brackets and does not convert anything.

list("abc")      # ['a', 'b', 'c']
list(range(3))  # [0, 1, 2]
["abc"]          # ['abc'], one string as one item

Use list(...) to convert something that is not a list yet, such as a string, a range or a tuple. Methods like split() return lists as well, which is covered in how to split a string with a delimiter in python.

Speed

[] is usually a little faster. Python can build the empty list in one step, while list() first looks up the name list and then calls it. The difference is small and rarely matters, but you can test it on your own machine with timeit:

import timeit

print(timeit.timeit("[]", number=1_000_000))
print(timeit.timeit("list()", number=1_000_000))

Shadowing the name list

Your own code can rebind the name list. After that, list() may stop working, but [] still works.

list = [1, 2, 3]
list()  # TypeError: 'list' object is not callable

Avoid naming a variable list, which is a common source of this error.

Comparing with C++

Python’s list is a dynamic array, so the closest C++ type is std::vector, not std::list. C++ has no bracket literal for vectors, but braces do similar work:

#include <vector>

std::vector<int> a;           // empty, like []
std::vector<int> b{1, 2, 3};  // like [1, 2, 3]
std::vector<int> c(3, 0);     // three zeros, like [0] * 3
std::vector<int> d{3};        // one element, the value 3

The last two lines show a pitfall. Parentheses and braces mean different things, so c(3, 0) builds three zeros, while d{3} builds a vector holding the single value 3. For all the ways to fill a vector, see c vector initialization.

Summary

  • Use [] for a new empty list, or to write out the items.
  • Use list(...) to convert another iterable.
  • Both give an equal, mutable list, and [] is usually a little faster.

A similar trap shows up with sets, where the answer is different: Is set() the same as {}? No, because {} is an empty dict.

The complete guide to list vs array

List vs Array in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Rust vs C++ Performance

Rust and C++ both compile to fast native code, so speed gaps are often small. Learn what drives performance in each, and how to read benchmarks fairly.

Rust vs C++ 6 min read