How to Return an Empty List from a Function in Python?

5/5 - (3 votes)

Method 1: Return a New Empty List Using Square Brackets

You can return an empty new list from a Python function using the square bracket notation [] after the return keyword using the expression return [].

Here’s an example:

def empty_list():
    return []


l1 = empty_list()
l2 = empty_list()

print(l1)
# []

print(l2)
# []

print(l1 is l2)
# False

The example also shows that the two created empty lists using two function calls point to different objects in memory, i.e., they are not the same objects. This is important if you want to maintain multiple list objects in your code to fill them with values later.

I’d recommend you check out the following three tutorials to get some background understanding of important concepts used in the code:

🌍 Recommended Tutorials:

  1. Python Membership Operator (“in“)
  2. How to Create an Empty List in Python?
  3. The return Keyword in Python

Method 2: Return a New Empty List Using list()

You can return an empty new list from a Python function using the Python built-in function list() after the return keyword using the expression return list().

Here’s an example:

def empty_list():
    return list()


l1 = empty_list()
l2 = empty_list()

print(l1)
# []

print(l2)
# []

print(l1 is l2)
# False

This is largely similar to the previous method although you’d never do something like this as you could simply call list() instead of empty_list().

πŸ‘‰ Recommended Tutorial: Python list() — A Helpful Guide

However, I wanted to show this approach for comprehensibility. Let’s dive into a more interesting example next!

Method 3: Empty a List Given as an Argument Using clear()

If you want to create a function that returns an empty list starting with a possibly non-empty list arg_list passed as a function argument, you can use the expression arg_list.clear() that removes all elements from the list object. You can then return the arg_list that is now empty.

Here’s an example:

def empty_list(full_list):
    full_list.clear()
    return full_list


my_list = empty_list(['Alice', 'Bob', 'Carl', 42])
print(my_list)
# []

I don’t think this is the clearest approach to accomplish this though because if you modify an argument, you shouldn’t return anything. This would signal to the “outside” that the function merely modifies an existing object rather than creating a new one.

In any case, feel free to check out my tutorial with video on the Finxter blog:

πŸ‘‰ Recommended Tutorial: Python List clear() — A Helpful Guide

We’re not done yet. In the final method, let’s discuss how you could also remove all elements one by one. This would give you more control about which elements to remove — and which not (although we assume we want to remove all elements in this tutorial).

Method 4: Empty a List Given as an Argument Using pop()

You can use Python’s pop() list method multiple times to remove all elements from a given list argument and, thereby, making it empty. Return the empty list and you’re done!

Here’s an example:

def empty_list(full_list):
    n = len(full_list)
    for _ in range(n):
        full_list.pop()
    return full_list # (now empty ;)


my_list = empty_list(['Alice', 'Bob', 'Carl', 42])
print(my_list)
# []

Summary

The four best ways to return an empty list from a Python function are:

  1. return []
  2. return list()
  3. my_list.clear() and return my_list
  4. Iterate my_list.pop() and return my_list

If you want to return a non-empty list, I’d recommend you check out our full tutorial here:

πŸ‘‰ Recommended Tutorial: Python Return List From Function


Feel free to join my free Python and crypto email academy for continuous improvement in coding, and download our free cheat sheets: πŸ‘‡