How to Return an Empty List in Python?

Estimated read time 1 min read

To return an empty list in Python, you can simply create a new empty list and return it. Here’s an example:

def get_empty_list():
    empty_list = []
    return empty_list

result = get_empty_list()
print(result)  # Output: []

In this example, the get_empty_list() function creates an empty list named empty_list and returns it. When the function is called, the returned empty list is stored in the result variable. Printing result will display an empty list [].

You can also use the built-in list() constructor to create an empty list and return it:

def get_empty_list():
    return list()

result = get_empty_list()
print(result)  # Output: []

In this case, the list() constructor is called without any arguments, which creates an empty list. The empty list is then returned from the function and assigned to the result variable.

Both approaches will return an empty list, and you can choose the one that is most convenient for your code.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply