How to pass a list as an argument in Python

Using Python to pass a list as an argument should be easy to understand after reading this tutorial. A function can receive arguments in the form of information to be passed to it. Following the function, names are the arguments enclosed in parentheses. The number of arguments can be added to the function as many times as you desire, but be sure to separate them with a comma so that they can be used independently from each other. The topic in this tutorial has been explained with the help of an example, so make sure you read the text carefully to fully understand what it is about.

List as an argument in Python

In this example, we will pass a list as an argument in which the list contains a list of colors. Look at the code given below:

#call the function
def list_argu(cl):
#using for loop to execute it to the size of a list
    for i in cl:
        print(i)
    return
#main program
color = ["Red", "Green", "Blue", "White", "Black"]
list_argu(color)

Output:

Red
Green
Blue
White 
Black

There are several steps required to create this program. It starts with defining the function or calling it. Next, it uses a for loop to print the values and finally, it returns the function. As you can see in the code above, the main program basically consists of creating a list and then printing the function as shown above. When the output is obtained, it will show the arguments for the list in Python, and the program will be executed 5 times, considering that there are 5 values in the list, and the function will be returned after the program has been executed five times.

Leave a Reply

Your email address will not be published. Required fields are marked *