In this article, we will discuss how an array or list can be passed to a function as a parameter in Python.
Pass an array to a function in Python
So for instance, if we have thousands of values stored in an array and we want to perform the manipulation of those values in a specific function, that is when we need to pass an entire array to the specific function.
Syntax: array(data_type, value_list)
Let us understand this with the help of a couple of examples
Example 1:
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.
Python3
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
Time complexity: O(n), where n is the length of the input array.
Auxiliary space: O(1).
Example 2:
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.
Python3
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
Time complexity: O(n), where n is the length of the input array ‘arr’.
Auxiliary space: O(1), as only a single variable ‘p’ is used for computation and it does not depend on the size of the input.
Pass a list to a function in Python
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: var_name = [ele1, ele2,..]
A list is created by placing elements inside square brackets [], separated by commas.
Let us understand this with the help of a couple of examples
Example 1:
A list named animals have been created, and the animal list which we created earlier is passed entirely to a user-defined function named print_animals where we will be printing all the elements of the list of animals. A user-defined function named print_animals is created that gets the list of animals as the input. Its simple purpose is to print every element f the animal’s list using for a loop.
Python3
def print_animals(animals): for a in animals: print (a) # Created a list of type string animals = [ "Cat" , "Dog" , "Tiger" , "Giraffe" , "Wolf" ] # passing the entire list as a parameter print (animals) |
Output:
['Cat', 'Dog', 'Tiger', 'Giraffe', 'Wolf']
Time complexity: O(n) where n is the length of the animals list
Auxiliary space: O(1) .
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.
Python3
def Product(nums): p = 1 for i in nums: # Multiplication of every element # of nums with each other p * = i print ( "Product: " , p) # Created a list of integers nums = [ 4 , 5 , 6 ] # Passed the entire list as a parameter Product(nums) |
Output:
Product: 120
Example 3:
Now there might be a case where the elements of the list are not decided already. This means that number of elements and the values are not determined. In this case, we will pass values as parameters which in turn will act as a list i.e. collection of values.
*args: It is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list.
Python3
# User defined function taking the # values as input def Product( * arguments): p = 1 for i in arguments: # Multiplying each and every element p * = i # Printing the final answer which # is their multiplication print (p) # Passing values that we want in our list Product( 4 , 5 , 1 , 2 ) |
Output:
40