Open In App

How to pass an array to a function in Python

Last Updated : 21 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Arrays in Python are implemented using the array module, which allows you to store elements of a specific type. You can pass an entire array to a function to perform various operations.

Syntax to create an array

array(data_type, value_list)

In this article, we'll explore how to pass arrays and lists to functions, with practical examples for both.

Example 1: Iterate Over an Array

We need to import an array, and after that, we will create an array with its datatype and elements, and then we will pass it to the function to iterate the elements in a list. 

Python
from array import *

def show(arr):
    for i in arr:
        print(i, end=', ')

arr = array('i', [16, 27, 77, 71, 70, 75, 48, 19, 110])
show(arr)

Output
16, 27, 77, 71, 70, 75, 48, 19, 110, 

Explanation: show() function receives an array and iterates over it using a for loop to print each element.

Example 2: Multiply All Elements of an Array

We need to import an array, and after that, we will create an array and pass it to a function to multiply the elements in a list.

Python
from array import *

def Product(arr):
    p = 1
    for i in arr:
        p *= i
    print("Product: ", p)

arr = array('f', [4.1, 5.2, 6.3])
Product(arr)

Output
Product:  134.31599601554856

Passing a List to a Function

The passing of parameters is not restricted to data types. This implies that variables of various data types can be passed in this procedure. So for instance, if we have thousands of values stored in a list and we want to manipulate those values in a specific function, we need to pass an entire list to the specific function. 

Syntax to create a list:

var_name = [ele1, ele2, ...]

Let’s explore how to pass lists to functions in Python:

Example 1: Print All Elements of a List

Python
def print_animals(animals):
    for a in animals:
        print(a)

ani = ["Cat", "Dog", "Tiger", "Giraffe", "Wolf"]

print_animals(ani)

Output
Cat
Dog
Tiger
Giraffe
Wolf

Explanation: print_animals() function takes a list animals and prints each animal using a for loop.

Example 2: Find the Product of All Numbers in a List

In a similar fashion let us perform another program where the list will be of type integers and we will be finding out the product of all the numbers present in the list.

Python
def Product(nums):
    p = 1
    for i in nums:
        p *= i
    print("Product: ", p)

l = [4, 5, 6]

Product(l)

Output
Product:  120

Example 3: Using *args to Pass a Variable Number of Arguments

Sometimes, the number of elements in a list isn’t fixed beforehand. In such cases, you can use *args to pass a variable number of arguments to a function.

Python
def Prod(*arguments):
    p = 1
    for i in arguments:
        p *= i
    print("Product: ", p)

Prod(4, 5, 1, 2)

Output
Product:  40

Explanation: *args collects all the arguments passed to the function into a tuple.


Next Article

Similar Reads