How To Pass A List To A Function In Python

One of the most useful features of Python is its ability to handle different data structures, such as lists.

Lists are a powerful and highly versatile tool that can store a sequence of values in one data structure. When working with functions, you can pass a list as an argument to perform various actions on its elements.

In this tutorial, we will learn how to pass a list to a function in Python and understand some examples.

Step 1: Define a Function with a List Parameter

Creating a function that accepts a list as an argument is similar to creating any regular function. In the function definition, we must include a parameter that will hold the list we’re passing.

Here’s an example of a simple function that takes a list as a parameter and prints its elements:

Step 2: Create a List to Pass to the Function

To pass a list to the function, first, we need to create a list. You can create a list in Python using square brackets [] and separating the elements with commas.

Here’s an example of creating a simple list with integer elements:

Step 3: Call the Function with the List as an Argument

To pass the list to the function, simply call the function followed by the list variable’s name within the parentheses.

For our example, we will call the print_list_elements function with the numbers list as an argument:

This will give us the following output:


Step 4: Manipulate the List Elements within the Function (Optional)

You can perform various operations on the list of elements within the function. You can add, remove, or modify the elements, or even apply calculations before working with them.

Here’s an example of a function that calculates the square of each element in a list and returns a new list containing the results:

Now, let’s call this function with the numbers list as an argument:

This will give us the following output:

[100, 400, 900, 1600, 2500]

Full Code

Here is the full code for the tutorial:


In this tutorial, we learned how to pass a list as an argument to a function in Python. We also explored some examples of functions that manipulate the list and return new results. By using lists as function parameters, we can simplify our code and make it more efficient and readable.