How to pass a Group of elements to Python Function?

How to pass a Group of elements to Python Function?

In this chapter, we will continue to discuss the concept of Functions and the topic of interest is “How to pass Group of elements to Python Function”. As a pre-requisite, we suggest that you go through the previous chapters based on Python functions to understand this concept. We have added here the link to all the Python Chapters: Learn Python.

How to pass a Group of elements to Python Function?

If you want to pass a group of elements such as strings or numbers, we can accept them into a list and pass the same list to our desired function. Let us now look at a program to understand more about this concept.

#Python program to pass group of numbers to function
#let us define our function that
#can accept list which contains
#group of numbers
def numbers(list1):
    """Find sum and average"""
    num=len(list1)
    total=0
    print("Inside the 'numbers()' function")
    for element in list1:
        total+=element
        average=total/num
    print("Operation processed")
    return total,average
#obtain group of numbers as input from keyboard
print("Enter each number separated by space: ")
list1=[int(a) for a in input().split()]
#let us call the numbers function
#and pass the list1
p,q=numbers(list1)
print("************")
print("Outside the 'numbers()' function")
print("Please find the output below")
print("Total sum: ",p)
print("Average is: ",q)

Output

Enter each number separated by space: 
3 4 5 
Inside the 'numbers()' function
Operation processed
************
Outside the 'numbers()' function
Please find the output below
Total sum:  12
Average is:  4.0

In the above program, we declared a numbers(list1) function that accepts a list of numbers and also added within it the logic to calculate the total and average of the numbers in the list. This function returns the total and average as mentioned towards the end of its block.

Outside the function, we declared a list-list1 by letting users enter its elements from the keyboard. We used the code, list1=[int(a) for a in input().split()] for the same.

After declaring the list, we called the numbers(list1) function and passed the values entered from the keyboard. As you know, since this function returns the sum and average of the number elements passed to it, we have declared p and q variables accordingly to these values. The code p,q=numbers(list1) means that total and average values returned by the numbers function after processing the elements in the list1 will be assigned to the variables p & q respectively.

In the following lines of the code in the program above, we print the sum and average values, respectively.

Let us now look at a similar program that handles the acceptance of a group of string values via a list into a function.

#Python program to pass group of string values to function
#let us define our function that
#can accept list which contains
#group of strings
def output1(list1):
    """Display the strings"""
    print("Inside the function")
    for element in list1:
        print(element)
#obtain group of numbers as input from keyboard
print("Enter each string separated by comma: ")
list1=[a for a in input().split(',')]
#let us call the output function
#and pass the list1 with strings
output1(list1)
print("**********")
print("End of the program")

Output

Enter each string separated by comma: 
India,Australia,England
Inside the function
India
Australia
England
**********
End of the program

In the above program, we declared an output1(list1) function that accepts a list of strings and also added within it the logic to display each string entered by user from the keyboard. When we call this function by passing strings to it, it will display each string passed through the list due to the print() function mentioned in the function block.

Outside the function, we declared a list-list1 by letting users enter the string elements in it from the keyboard. We used the code, list1=[a for a in input().split(‘,’)] for the same.

In the following line of code, we passed the list1 to the output1 function, and it printed the entered strings separately as per the logic in the function block.

You can use the programs above to pass either group of numbers or strings in your Python program. 

Source

Scroll to Top