How to Find the average of two list using Python?


Introduction

Python is popular worldwide because of its simplicity and flexibility with other applications. In the 21st century, handling data is the most challenging task for organizations with a high volume of data, and with the development of data science and machine learning it has become easier to access. The List is a data structure consisting of changeable elements after the initialization. The lists are usually assigned a value within a brackets “[]” in some ordered form.

To Find the average of two lists using Python

In this article, we will explore various techniques for finding the average of two lists using Python. The methods used to find the average are mean() function using numpy and statistics library, for loop and sum() function.

Syntax

sum()  

It is a built-in Python function from the math module used to add all the given list of numbers.

len() 

It is also a function from the math module that returns the total length of the given list of numbers.

mean() 

Approach

Approach 1 − Utilizing the statistics module

Approach 2 − Utilizing the lambda function

Approach 3 − Utilizing the math module

Approach 1: Python Program to find the average of two lists using the statistics module

The statistics library is imported and the two lists are initialized with three elements. One of the functions used in the statistics module is mean() function. Then the average_of_lists is printed to return the list of values.

Algorithm

  • Step 1 − The required modules to include the functions are statistics.

  • Step 2 − Two lists are initialized with a set of elements.

  • Step 3 − To find the average, the two lists of elements are added together and then dividing it by 2.

  • Step 4 − The normal formula for finding the average is not used as the mean() function is used.

  • Step 5 − The average or mean of the two lists is printed.

Example

#importing the statistics library
import statistics
# Two list data structures are used to initialize the value.
num_1 = [56, 34, 90]
num_2 = [23, 87, 65]

#statistics module uses the mean function to find the average of two lists
avg_of_lists = statistics.mean(num_1 + num_2)
#returns the average value of the list
print("Mean of the value", avg_of_lists)

Output

Mean of the value 59.166666666666664

Approach 2: Python Program to find the average of two lists using lambda function

The numpy library is imported and the two lists are initialized with three elements. In the case of a function, we use the def function but for the anonymous function, we can use the lambda function along with the key parameter. The lambda function usually comes along with either filter() or map() function.

Algorithm

  • Step 1 − The required modules to include the functions are numpy.

  • Step 2 − Two lists are initialized with a set of elements.

  • Step 3 − To find the average, the two lists of elements are added together and then dividing it by 2.

  • Step 4 − Then it is converted back to the list using the map() function along the key parameters.

  • Step 5 − The average or mean of the two lists is printed.

Example

#importing the numpy library
import numpy as np 
## Two list data structures are used to initialize the value.
num_1 = [56, 34, 90]
num_2 = [23, 87, 65]

#lambda function is used to convert the lists into averages and stores it in the variable avg_value.
avg_value = np.array(list(map(lambda a, b: (a + b) / 2, num_1, num_2)))

#returns the average value of the list
print("Mean of the value", avg_value.mean())

Output

Mean of the value 59.166666666666664

Approach 3: Python Program to find the average of two lists using math module

The math library is imported and the two lists are initialized with three elements. The math module is used to deal with the regular expression for calculating the average. Then the average_of_lists is printed to return the list of values.

Algorithm

  • Step 1 − Include the required module.

  • Step 2 − Two lists are initialized with a set of elements.

  • Step 3 − The normal formula for finding the average is not used as the sum() and len() functions are used.

  • Step 4 − The average or mean of the two lists is printed.

Example

#importing the math library
import math

# Two list data structures are used to initialize the value.
num_1 = [56, 34, 90]
num_2 = [23, 87, 65]
#Regular expression to find the average of two lists
avg_of_lists = (sum(num_1) + sum(num_2)) / (len(num_1) + len(num_2))
#returns the average value of the list
print("Mean of the value", avg_of_lists)

Output

Mean of the value 59.166666666666664

Conclusion

It can be useful to users on a daily basis irrespective of their background which makes programming more successful in terms of accuracy, time, and performance wise too hence can implement similarly during on-site projects or during academic research studies without any limitations.

Updated on: 25-Aug-2023

288 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements