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 *=
    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 *=
     
    # 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


How to pass an array to a function in Python

In this article, we will discuss how an array or list can be passed to a function as a parameter in Python

Similar Reads

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....

Pass a list to a function in Python

...