How to Return List in Python?

Several features make Python an ideal programming language for developers. Python’s ability to return lists is one of its most useful features. A list is a fundamental Python data structure that stores and manipulates data collections. 

Python’s return list feature is used to create powerful and efficient code. Considering its importance, this post provides various ways to return a list using the following contents:

Method 1: Using the return Statement

The following examples are used to return a Python list in user-defined function:

Code:

def list_func():
    list1 = []
    for i in range(5):
        list1.append(i)
    return list1
num = list_func()
print(num)
  • The user-defined function named “list_func” is defined.
  • The empty list named “list1” is initialized.
  • The for loop iterates over the specified range and appends each range element to an empty list.
  • The list is returned to the function using the return keyword.
  • The value of the list is accessed by accessing the function.

Output:

The list has been returned successfully.

Example 1: Return a Python List as a String

The below code is used to return a Python list as a string:

Code:

def list_func(list_value):
    return' '.join([str(i) for i in list_value])

print(list_func(['Joseph', 'Anna', 'Lily']))
  • The user-defined function named “list_func” is defined in the program.
  • The “str()” function is used to convert the elements of the list into strings and join them into an empty string using the “join()” function.

Output:

The list has been returned as a string.

Example 2: Return a Python List of Tuples

The below code is used to return a Python list of tuples:

Code:

def list_func(x, y):
    return list(zip(x, y))
print(list_func(['Joseph', 'Lily', 'Anna'], [15, 32, 45]))
  • The function named list_func takes two lists x and y as input. 
  • The function retrieves a list of tuples where each tuple contains the corresponding elements of the two input lists.
  • The zip function combines the two lists into a list of tuples. The list function is utilized to convert the result into a list.

Output:

The list of tuples has been returned successfully.

Method 2: Using the List Comprehension

The below example code is used to return a list using list comprehension:

Code:

def list_func():
    return [i for i in range(0,10,3)]
num = list_func()
print(num)
  • The list comprehension is used inside the user-defined function to return a list.
  • The list is accessed by accessing the function.

Output:

The value of the list has been displayed.

Method 3: Using Lambda Function

The lambda function is defined without a name and using the keyword lambda. The below code is used to return a list by using the lambda function:

Code:

list_value = lambda : [i for i in range(0, 15, 3)]
print(list_value())

In the lambda function, the list comprehension is used along with the range() function to create a list of numbers from 0 to 15 with a step of 3.

Output:

The list has been returned successfully.

Conclusion

To return a Python list to the function, the return statement, list comprehension, and lambda function are used in Python. The return statement returns the list individually and with the help of list comprehension. The lambda anonymous function is also used to return a Python list with the help of list comprehension. This guide has provided all the methods to return lists in Python.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×