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 arrayarray(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 ArrayWe 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)
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 ArrayWe 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)
Product: 134.31599601554856Passing 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)
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 ListIn 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4