The Wayback Machine - https://web.archive.org/web/20240804145458/https://www.geeksforgeeks.org/how-to-pass-an-array-to-a-function-in-python/
Open In App

How to pass an array to a function in Python

Last Updated : 26 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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


Similar Reads

How to write an empty function in Python - pass statement?
In C/C++ and Java, we can write empty function as following // An empty function in C/C++/Java void fun() { } In Python, if we write something like following in Python, it would produce compiler error. # Incorrect empty function in Python def fun(): Output : IndentationError: expected an indented block In Python, to write empty functions, we use pa
1 min read
Python - pass multiple arguments to map function
The map() function is a built-in function in Python, which applies a given function to each item of iterable (like list, tuple etc.) and returns a list of results or map object. Syntax : map( function, iterable ) Parameters : function: The function which is going to execute for each iterableiterable: A sequence or collection of iterable objects whi
3 min read
Pass function and arguments from node.js to Python
Prerequisites: How to run python scripts in node.js using the child_process module. In this article, we are going to learn how to pass functions and arguments from node.js to Python using child_process. Although Node.js is one of the most widely used web development frameworks, it lacks machine learning, deep learning, and artificial intelligence l
4 min read
How to pass optional parameters to a function in Python?
In Python, when we define functions with default values for certain parameters, it is said to have its arguments set as an option for the user. Users can either pass their values or can pretend the function to use theirs default values which are specified. In this way, the user can call the function by either passing those optional parameters or ju
5 min read
How to pass multiple arguments to function ?
A Routine is a named group of instructions performing some tasks. A routine can always be invoked as well as called multiple times as required in a given program.  When the routine stops, the execution immediately returns to the stage from which the routine was called. Such routines may be predefined in the programming language or designed or imple
5 min read
Loops and Control Statements (continue, break and pass) in Python
Python programming language provides the following types of loops to handle looping requirements. Python While Loop Until a specified criterion is true, a block of statements will be continuously executed in a Python while loop. And the line in the program that follows the loop is run when the condition changes to false. Syntax of Python Whilewhile
4 min read
Pass list as command line argument in Python
The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. One of them is sys module. sys Module A module is a file containing Python definitions and statements. The sys module provides functi
3 min read
Python | Split and Pass list as separate parameter
With the advent of programming paradigms, there has been need to modify the way one codes. One such paradigm is OOPS. In this, we have a technique called modularity, which stands for making different modules/functions which perform independent tasks in program. In this, we need to pass more than just variable, but a list as well. Let's discuss cert
4 min read
Difference between continue and pass statements in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scop
3 min read
break, continue and pass in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from their normal sequence. When execution leaves a sc
5 min read
three90RightbarBannerImg