Return List From Function in Python: 4 Things You Should Know

Do you know how to return a list from a function in Python? This tutorial will show you how to do that with simple examples.

As a Python developer, you might want to generate and return a list from a function. To do that you can create a list in your function and then return it to the caller of the function using a return statement. The approach is the same as if you were returning any other variable from a function.

In this article, you will learn a basic approach to return lists from functions in Python and a few other concepts related to this topic that you will find useful when coding.

Let’s get started!

1. Can You Return a List in Python?

In Python, you can return a list from a function using a return statement after creating the list in the function.

Here is a simple example of a Python function that returns a list:

def get_numbers():
    numbers = [1, 2, 3, 4, 5]
    return numbers

result = get_numbers()
print(result)

In this code snippet, we first define a function called get_numbers() using the def keyword. This function creates a list called numbers and then returns it using the return keyword.

We then store the return value of the function in the variable result. We then pass the variable result to the print() function that prints the list returned by the function.

The output of this code is the following:

[1, 2, 3, 4, 5]

This is a basic example of how to return a list in Python.

2. How to Return Multiple Lists From a Function in Python

A Python function can return multiple lists. To do that you have to provide the lists to the return statement of the function using the comma as a separator.

Let’s see how this works with a code example:

def get_data():
    numbers = [1, 2, 3, 4, 5]
    words = ['one', 'two', 'three', 'four', 'five']
    return numbers, words

On the last line of the function get_data() we return two lists, one that contains integers and one that contains strings.

What data type do you think is returned by the function?

We can find this out using the type() built-in function.

data = get_data()
print(type(data))

[output]
<class 'tuple'>

You can see that the function we created returns a tuple.

So, where are the two lists we returned in the last line of the function?

The two lists returned by the function are inside the tuple.

Let’s confirm it by printing the value of the variable returned by the get_data() function:

data = get_data()
print(data)

[output]
([1, 2, 3, 4, 5], ['one', 'two', 'three', 'four', 'five'])

You can see that the function returns a tuple in which the first element is the list of numbers and the second element is the list of words returned by the function.

You can access each list in the tuple by using indexing. Below we access the first and second elements (both lists) in the tuple:

First element

data = get_data()
print(data[0])

[output]
[1, 2, 3, 4, 5]

Second element

data = get_data()
print(data[1])

[output]
['one', 'two', 'three', 'four', 'five']

Remember that sequence indexing in Python starts from zero.

3. How to Return a Python List Created Using a List Comprehension

In Python, it’s also possible to create a list using a list comprehension in a function that then returns the list created.

One possible scenario in which you would create a list with a list comprehension as part of a function is if you want to generate a list that follows a specific pattern.

Two examples could be:

  • a list of numbers from 1 to 10
  • even numbers from 1 to 10

Let’s write some code that returns a list of numbers from 0 to 10 using a list comprehension and the range() function:

def get_numbers():
    return [x for x in range(11)]

Now call the function and confirm that it returns the correct list:

numbers = get_numbers()
print(numbers)

[output]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In the last snippet of Python code, you called the get_numbers() function and stored the list returned by the function in the numbers variable.

We then used the print() function to print the elements in the numbers list.

4. How Do You Verify If a Python Function Returns a List?

When writing a Python application you might want to be sure that your function is creating a list correctly before returning it.

In the examples we have seen previously, it’s relatively easy to confirm that the object we created in the functions is a list due to the fact that the code in the functions is quite simple.

But…

When you write real programs you might end up with complex functions in which it’s not so straightforward to see or confirm if the object you are returning is a list.

In a Python function, to confirm that you are actually returning a list, you can use Python’s type() built-in function. This function shows the data type of the variable you are returning and confirms if that’s a list.

Here is an example:

def get_words():
    message = "This is a test message"
    words = message.split()
    print(type(words))
    return words

You can see from the code of the function that we are creating a list from a string using the string split() method.

Before returning this list we are using the type() function to print the data type of the words variable.

Let’s see what happens when we call this function:

words = get_words()

[output]
<class 'list'>

The output confirms that the variable returned by the function get_words() is a list.

You can use the print() function to see the content of the list the function returns:

print(words)

[output]
['This', 'is', 'a', 'test', 'message']

You can see that this list is the result of splitting the initial string using the space as a separator.

Conclusion

Returning a list from a function in Python is a common practice that allows returning multiple values using a single data structure.

In this tutorial, we have seen how to return a single list and multiple lists from a function. We have also seen how to return a list created using list comprehensions.

You also learned how to verify that the variable you return from a function is actually a list.

Related article: And now continue building your Python knowledge by going through the step-by-step Codefather tutorial about Python functions.

Leave a Comment