How To Return Empty List in Python

In this tutorial, we will learn how to return an empty list in Python. An empty list is a list that contains no elements. We will cover different methods to return an empty list, and you can choose which approach best suits your needs.

Method 1: Creating an empty list using brackets []

The most straightforward method to create an empty list in Python is to use empty square brackets []. This approach is easy to read and understand. Here is an example:

Output:

[]

Method 2: Creating an empty list using the list() constructor

Another way to create an empty list is by using the built-in list() function without any arguments.

Output:

[]

Method 3: Returning an empty list in a function with a conditional statement

In some cases, you may want to return an empty list depending on the condition. You can use a conditional statement followed by the return keyword to achieve this.

Output:

[]
[1, 2, 3]

You can use a similar approach with the list() function if you prefer:

Output:

[]
[1, 2, 3]

Conclusion

You have now learned multiple ways to return an empty list in Python. You can create an empty list using square brackets [], the list() function, or using conditional statements depending on your needs. Now you can choose the method that best fits your specific use case and integrate it into your code.