How to pass an array to a function in Python
Last Updated :
21 Jun, 2025
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)
Output16, 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)
OutputProduct: 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)
OutputCat
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)
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)
Explanation: *args collects all the arguments passed to the function into a tuple.
Similar Reads
Pass a List to a Function in Python In Python, we can pass a list to a function, allowing to access or update the list's items. This makes the function more versatile and allows us to work with the list in many ways.Passing list by Reference When we pass a list to a function by reference, it refers to the original list. If we make any
2 min read
How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de
5 min read
How to Add Function in Python Dictionary Dictionaries in Python are strong, adaptable data structures that support key-value pair storage. Because of this property, dictionaries are a necessary tool for many kinds of programming jobs. Adding functions as values to dictionaries is an intriguing and sophisticated use case. This article looks
4 min read
How to write an empty function in Python - pass statement During the development of a Python program, we might want to create a function without implementing its behavior. In such cases, we can define the function and use the pass statement inside it to avoid syntax errors. To write empty functions in Python, we use pass statement. pass is a special statem
3 min read
pandas.array() function in Python This method is used to create an array from a sequence in desired data type. Syntax : pandas.array(data: Sequence[object], dtype: Union[str, numpy.dtype, pandas.core.dtypes.base.ExtensionDtype, NoneType] = None, copy: bool = True) Parameters : data : Sequence of objects. The scalars inside `data` sh
2 min read