Python lists are used to store multiple values in a variable. Python lists are created using square brackets. Tuple, Set, and Dictionary are other methods of storing a collection of data in Python.
In this article, we will learn how to return a list in Python. We will learn different ways to achieve this along with some other useful functionalities you can perform with lists. So, let’s start with the different methods to return a list in Python.
How to Return a List in Python
- Method 1. Return Python list from Function
- Method 2. Returning the Python list using a list comprehension statement
- Method 3. Returning Python lists using Lambda function
How to Return a List in Python – including Example Code Snippets
Method 1. Return a Python list from Function
In this method, we simply create a list inside the function. For this, assign an object (square brackets) to the list variable. After processing the list in the function i:e storing data in it, we return the list using the return keyword.
Example
def return_list(): my_list = [] for x in range(5): my_list.append(x) return my_list print(return_list()) # [0, 1, 2, 3, 4, 5]
In the above example, we have created the my_list variable and stored numeric values in it inside the loop. Finally, we return the list using the return keyword.
Remember, in this method, the list is accessible inside the function body. If you try to access it outside the function, Python will give NameError.
NameError: name 'return_list' is not defined
To access the list outside the function, you can call the function and assign it to a variable. Look at the example below.
result = return_list() print(result) # [0, 1, 2, 3, 4, 5]
Tip: The list.append() method adds a new value to the end of the Python list.
Method 2. Returning the Python list using a list comprehension statement
Offers a concise syntax to return the Python list. It produces the same result as the first method. It makes it easy to assign the values from a list to the new one. The best part about the List comprehension statement method is that we can achieve our goal in just one line of code.
Example
def return_list(): return [x for x in range(5)] result = return_list() print(result) # [0, 1, 2, 3, 4, 5]
In the above example, we specify the expression x for x in range(10) which is called a list comprehension statement. The x before the for is the identity expression. It receives the values from the loop and stores them in the new list.
The next method to return the Python list uses Lambda Expression.
Method 3. Returning Python lists using Lambda function
An interesting way to return a list in Python is using the lambda function. Before diving into it, let’s learn about the Lambda function.
Lambda functions
Lambda function is a shorter way to work with functions. It is an anonymous function that can take any number of arguments. Go through the following syntax and example to have a better understanding of the Lambda function.
Syntax
lambda arguments : expression
def my_function(n): return lambda a : a * n my_doubler = myfunc(5) print(my_doubler(7))
Now, we are good at coding this method. We create an anonymous function using lambda statements and assign it to the variable new_list. Note, we have used a combination of the list comprehension along with this method. You can also try it without the combination.
Example
list = lambda : [x for x in range(5)] result = list() print(result) # [0, 1, 2, 3, 4, 5]
We have presented three methods to return a Python list. You must try all of these and practice with the given examples to master them. For deep understanding, follow the next section covering some additional list functions that will help you work with Python lists more efficiently.
Python list methods
In this section, you will learn about all the list methods and their uses.
Function Name | Description |
append() | Add a new element to the end of the list. |
copy() | Returns the copy of the list |
clear() | Make the list empty by clearing all elements |
count() | Get the count of the specified list element |
extend() | Add the element of any iterable or list to the end of the list |
index() | Returns the very first index of the specified element found in the list |
insert() | Add a new element to the end of the list. Different from append() function because it takes an index argument to add the new element at. |
pop() | Removes the specified element from the list. |
remove() | Removes the very first specified element found in the list. |
reverse() | Reverse the elements of the list |
sort() | Sorts the elements of the list |
Accessing Python list elements
In this section, you will learn how to access the list values. There are two methods to do so.
- List index
- Inverted index
List index
Accessing list elements using an index is quite simple as accessing the array value. Pass the index value to the list variable, it will return the element present at that index.
my_list = ['e', 'l', 'e', 'm', 'e', 'n', 't'] # first item print(my_list[0]) # e # third item print(my_list[2]) # l
There can be nested elements in the list. For this, we access them in the following way.
# Nested List nested_list = ["Python", [2, 0, 2, 2]] # Nested indexing print(nested_list[0][1]) print(nested_list[1][3])
Note: The index value is always an integer. Using 1.0 or 0.1 as an index will produce an error.
Inverted index
We can use negative indices to access the list elements. Unlike the positive indices, it flows from right to left. -1 is the rightmost index and moves towards the left with -2, -3, and so on.
# Negative indexing in lists my_list = ['e', 'l', 'e', 'm', 'e', 'n', 't'] # last item print(my_list[-1]) # fifth last item print(my_list[-5])
Hey guys! It’s me, Marcel, aka Maschi. On MaschiTuts, it’s all about tutorials! No matter the topic of the article, the goal always remains the same: Providing you guys with the most in-depth and helpful tutorials!