Python
programming
lists
coding
tutorial

Creating an empty list in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Creating an empty list looks trivial in Python, but it shows up everywhere: collecting results, building a queue, accumulating errors, or preparing a mutable value for later use. Because lists are mutable, the way you create and reuse them has real consequences for correctness.

In practice, there are two standard ways to make an empty list: [] and list(). Both work, but [] is the normal choice because it is shorter and slightly more direct.

The Two Standard Ways

The most common pattern is the literal syntax:

python
items = []

You can also use the constructor:

python
items = list()

These produce the same result: a new empty list object. For most code, prefer [] because it is easier to read and is the idiomatic Python style.

Why an Empty List Is Useful

An empty list is often the starting point for incremental work. You create it once, then append values as they become available.

python
1numbers = []
2
3for value in range(5):
4    numbers.append(value * 10)
5
6print(numbers)

This prints:

python
[0, 10, 20, 30, 40]

That pattern is common when the final size is not known in advance. It is also useful when filtering data:

python
1names = ["Ana", "Ben", "Chris", "Dee"]
2short_names = []
3
4for name in names:
5    if len(name) <= 3:
6        short_names.append(name)
7
8print(short_names)

In many cases, a list comprehension is even cleaner, but understanding the empty-list pattern is still important because it makes list mutation explicit.

Empty Lists in Functions

A very common use case is collecting values inside a function and returning them to the caller.

python
1def find_even_numbers(values):
2    result = []
3
4    for value in values:
5        if value % 2 == 0:
6            result.append(value)
7
8    return result
9
10
11print(find_even_numbers([1, 2, 3, 4, 5, 6]))

The empty list belongs inside the function because each call should get a fresh container. This matters a lot when writing helper functions or APIs.

If you use type hints, the intent becomes even clearer:

python
1def tokenize(line: str) -> list[str]:
2    tokens: list[str] = []
3    for part in line.split():
4        tokens.append(part.lower())
5    return tokens

Avoid Reusing the Same Mutable List by Accident

The real mistakes usually happen after the list is created. Assigning the same list to another variable does not copy it. Both names point to the same underlying object.

python
1original = []
2alias = original
3
4alias.append("shared")
5
6print(original)
7print(alias)

Both variables show the same content because there is only one list in memory. If you need a separate list, create a new one or make an explicit copy.

python
original = ["a", "b"]
copy_of_original = original.copy()
copy_of_original.append("c")

The Mutable Default Argument Trap

This is the most important pitfall related to empty lists. Do not use [] as a function default unless you want the same list to be shared across calls.

Bad example:

python
def add_item(item, bucket=[]):
    bucket.append(item)
    return bucket

That default list is created once, not once per call. The result is surprising:

python
print(add_item("x"))
print(add_item("y"))

The second call returns a list that already contains "x".

The correct version is:

python
1def add_item(item, bucket=None):
2    if bucket is None:
3        bucket = []
4
5    bucket.append(item)
6    return bucket

Now each call gets a fresh list unless the caller intentionally provides one.

Common Pitfalls

  • Using list as a variable name shadows the built-in list() constructor.
  • Assuming assignment copies a list. It only creates another reference.
  • Using [] as a default function argument can leak values across calls.
  • Building a list in a loop is fine, but for simple transformations a list comprehension is often shorter and clearer.
  • Reusing a list between unrelated operations can produce hidden state bugs. Create a fresh empty list when you need a fresh result.

Summary

  • '[] and list() both create a new empty list.'
  • '[] is the normal, idiomatic form in Python code.'
  • Empty lists are useful for collecting values incrementally with append.
  • Create the list inside a function when each call needs independent state.
  • Be careful with aliasing and mutable default arguments because they create subtle bugs.

Course illustration
Course illustration

All Rights Reserved.