Python List Declaration: Syntax and Examples
python list declaration: Learn how to declare lists in Python using literal syntax, the list() constructor, type hints, and comprehensions, with practical examples and...
Declaring a list in Python is one of the first operations you learn, but the choice of declaration method affects readability, type safety, and sometimes performance. This article covers the common ways to perform a python list declaration, when each is appropriate, and what to watch out for.
Declaring a List with Literal Syntax
The most direct way to declare a list is with square brackets and comma-separated values:
numbers = [1, 2, 3] names = ["alice", "bob", "carol"] empty = []
The literal syntax is the most readable and is the idiomatic choice for a fixed, known set of elements. Because the list is constructed in a single expression, it also avoids the overhead of calling a constructor. There is no functional difference between [] and list() for an empty list, but the literal is shorter and more explicit about the intended type.
Using the list() Constructor
The list() constructor creates a list from an iterable. This is useful when you need to convert another sequence type or generate a list from a range:
chars = list("hello") # ['h', 'e', 'l', 'l', 'o'] squares = list(range(5)) # [0, 1, 2, 3, 4]
The constructor also accepts no arguments, producing an empty list. In practice, you should prefer the literal [] for an empty list because it is more concise and does not require a function call. The constructor becomes necessary when the source is already an iterable, such as a tuple, set, or generator.
Declaring Lists with Type Hints
Modern Python code often uses type hints to document the expected element type. A list declaration with a type hint looks like this:
from typing import List scores: List[int] = [95, 87, 92]
In Python 3.9 and later, you can use the built-in list type directly:
scores: list[int] = [95, 87, 92]
Type hints do not change runtime behavior; they are used by static type checkers and IDEs. Declaring the element type makes the code easier to maintain, especially in larger codebases where the intended shape of data is not obvious. For an empty list, you can still provide a type hint:
users: list[str] = []
This signals that the list will eventually hold strings, even though it is momentarily empty.
Common Mistakes When Declaring Lists
A frequent mistake is using list as a variable name, which shadows the built-in constructor. For example:
list = [1, 2] another_list = list(range(3)) # TypeError: 'list' object is not callable
Avoid naming variables list or dict. Another mistake is confusing list multiplication with repetition. [0] * 5 creates a list with five zeros, but [[]] * 3 creates three references to the same inner list, which can lead to surprising mutations:
matrix = [[]] * 3 matrix[0].append(1) # matrix is now [[1], [1], [1]]
If you need independent inner lists, use a comprehension:
matrix = [[] for _ in range(3)]
Memory and Performance Considerations
The declaration method rarely affects runtime performance for small lists, but it matters when building large lists. A list comprehension is generally faster than a for loop that repeatedly calls append because the loop overhead is reduced and the list is preallocated. For example:
# Slower result = [] for i in range(1000): result.append(i * 2) # Faster result = [i * 2 for i in range(1000)]
The comprehension is also more readable. If you need to generate a list from an existing iterable, list(iterable) is the direct approach and is implemented in C, so it is efficient. For an empty list, the literal [] avoids the constructor call, but the difference is negligible.
Choosing the Right Declaration Approach
The decision depends on the source of the data and the need for type clarity. Use the literal syntax when you know the elements at write time. Use list() when you have an iterable that needs conversion. Use type hints whenever the list is part of a public API or a data structure that crosses module boundaries. For empty lists, prefer [] unless you also need a type hint, in which case [] with a hint is still the clearest option.
The table below summarizes the main declaration patterns:
| Pattern | Use case | Example |
|---|---|---|
[a, b, c] | Fixed known elements | [1, 2, 3] |
list(iterable) | Convert an iterable | list(range(5)) |
[] | Empty list | [] |
list[int] | Type-hinted list (Python 3.9+) | scores: list[int] = [] |
[expr for x in it] | Build from a transformation | [x**2 for x in range(5)] |
Choose the approach that makes the code's intent clear. Overusing the constructor for a literal list adds noise, while ignoring type hints in a large codebase can make refactoring harder.