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:
1 2 3 |
def print_list_elements(input_list): for element in input_list: print(element) |
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:
1 |
numbers = [10, 20, 30, 40, 50] |
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:
1 |
print_list_elements(numbers) |
This will give us the following output:
10 20 30 40 50
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:
1 2 3 4 5 |
def square_list_elements(input_list): squared_list = [] for element in input_list: squared_list.append(element ** 2) return squared_list |
Now, let’s call this function with the numbers
list as an argument:
1 2 |
squared_numbers = square_list_elements(numbers) print(squared_numbers) |
This will give us the following output:
[100, 400, 900, 1600, 2500]
Full Code
Here is the full code for the tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def print_list_elements(input_list): for element in input_list: print(element) def square_list_elements(input_list): squared_list = [] for element in input_list: squared_list.append(element ** 2) return squared_list numbers = [10, 20, 30, 40, 50] print_list_elements(numbers) squared_numbers = square_list_elements(numbers) print(squared_numbers) |
Conclusion
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.