How to call a list in a function python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will learn how to apply a function to each element of a Python list. Let’s see what exactly is Applying a function to each element of a list means:

    Suppose we have a list of integers and a function that doubles each integer in this list. On applying the function to the list, the function should double all the integers in the list. We achieve this functionality in the following ways:

    1. map() method.
    2. Using list comprehensions.
    3. lambda function

    Using map() method

    map() methods take two arguments: iterables and functions and returns a map object. We use list() to convert the map object to a list.

    Program:

    Python3

    def double(integer):

        return integer*2

    integer_list = [1, 2, 3]

    output_list = list(map(double, integer_list))

    print(output_list)

    Output:

    [2, 4, 6]

    Time Complexity: O(n)*(O complexity of function applied on list)

    Using list comprehensions

    We use a list comprehension to call a function on each element of the list and then double it for this case.

    Program:

    Python3

    def double(integer):

        return integer*2

    integer_list = [1, 2, 3]

    output_list = [double(i) for i in integer_list]

    print(output_list)

    Output:

    [2, 4, 6]

    Time Complexity: O(n)*(O complexity of function applied on list)

    Using Lambda Function

    A lambda function can also be employed to produce the above functionality. Lambda is capable of creating an anonymous function that can be made enough to fit the given requirement.

    Program:

    Python3

    lst = [1, 2, 3]

    ans = []

    for x in lst:

        def res(x): return x*2

        ans.append(res(x))

    print(ans)

    Output:

    [2, 4, 6]

    1. HowTo
    2. Python How-To's
    3. Pass a List to a Function in Python

    Created: March-05, 2022

    1. Pass a List to a Function in Python
    2. Pass a List to a Python Function Just Like Any Other Data Type
    3. Difference Between Passing and Unpacking a List in Python
    4. Conclusion

    In Python, sending a list to a function is just like passing any other form of data. Let’s explore a little more into this subject.

    Pass a List to a Function in Python

    We will define a function testing with a single parameter, flavor. Then, we pass an argument named Cherry while calling the function.

    This argument goes to the parameter variable flavor, which the function can then use. See the example below.

    Code:

    def testing(flavor):
        print("You chose:", flavor)
    
    testing("Cherry")
    

    Output:

    You chose: Cherry
    

    Pass a List to a Python Function Just Like Any Other Data Type

    Python lists are like any other Python object that we can pass into a function as a simple variable. We have a function enjoy with a hobbies parameter in the code sample below.

    Outside the function block, we define a list hobbies_list. While calling the function enjoy, we pass this variable, hobbies_list, as an argument.

    This list goes to the parameter variable hobbies, and thus, the function can use the value of this list.

    Code:

    def enjoy(hobbies): #or def enjoy(hobbies=[]):
        for hobby in hobbies:
            print(hobby)
    
    hobbies_list = ['art', 'dance', 'sing']
    enjoy(hobbies_list)
    

    Output:

    art
    dance
    sing
    

    See how the enjoy function gets the value of the list, and the for loop inside it prints all the list items. Sometimes, you will also see the assignment of square brackets [] to the parameter variable while passing a list to a function.

    Difference Between Passing and Unpacking a List in Python

    In Python, we can use *args to pass a variable number of arguments to a function. Now, since a list has multiple values, people tend to use *args as the parameter variable for a list argument so that all the values of the list are taken care of.

    When we define *args as the parameter variable, we signal the function to wait for a variable number of arguments. Passing the elements of a list as multiple arguments is similar to unpacking a list.

    Code:

    def subjects(*args):
        for subject in args:
            print("The subject name is ",subject)
    
    names = ['mathematics', 'science', 'history']
    subjects(names)
    
    

    Output:

    The subject name is  ['mathematics', 'science', 'history']
    

    You will see the difference better if you compare this output with the below code.

    Code:

    def subjects(args):
        for subject in args:
            print("The subject name is ", subject)
    
    names = ['mathematics', 'science', 'history']
    subjects(names)
    

    Output:

    The subject name is  mathematics
    The subject name is  science
    The subject name is  history
    

    Notice how the output changes based on *args. If *args is used to pass a list to a Python function, we might not get the expected results.

    As a result, it’s important to select the appropriate syntax based on the requirements.

    Conclusion

    In this article, we learned about passing a list to a function in Python. We saw how we could pass a list to a function in Python just like we pass any other data type.

    We further understood the difference between passing and unpacking lists as multiple arguments.

    Related Article - Python Function

  • Exit a Function in Python
  • Optional Arguments in Python
  • Python Function Parameter Type
  • Imitate ode45() Function in Python
  • Can you pass a list to a function in Python?

    You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

    How do you input a list into a function in Python?

    Python input and output exercise..
    First, create an empty list..
    Next, accept a list size from the user (i.e., the number of elements in a list).
    Run loop till the size of a list using a for loop and range() function..
    use the input() function to receive a number from a user..

    How do you access a list from another function in Python?

    A Python function can return any object such as a list. To return a list, first create the list object within the function body, assign it to a variable your_list , and return it to the caller of the function using the keyword operation “ return your_list “.

    Can you call a function within a list Python?

    You can create the functions in the original scope, assign them to the array and then delete them from their original scope. Thus, you can indeed call them from the array but not as a local variable.