Python | Mean of tuple list
Last Updated :
05 Apr, 2023
Sometimes, while working with Python tuple list, we can have a problem in which we need to find the average of tuple values in the list. This problem has the possible application in many domains including mathematics. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loops The first approach that can be thought of to solve this problem can be a brute force approach in which we just loop each tuple to add element and then just divide it by number of tuples in the list.
Python3
test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )]
print ( "The original list is : " + str (test_list))
sum = 0
for sub in test_list:
for i in sub:
sum = sum + i
res = sum / len (test_list)
print ( "The mean of tuple list is : " + str (res))
|
Output
The original list is : [(1, 4, 5), (7, 8), (2, 4, 10)]
The mean of tuple list is : 13.666666666666666
Time complexity: O(n^2) where n is the total number of elements in the tuple list.
Auxiliary space: O(1) as we are only using a constant amount of extra space to store the sum variable.
Method #2 : Using chain() + sum() In order to reduce the line of codes, the chain() functionality can be used so that all the elements can be extracted and then can be added using sum().
Python3
from itertools import chain
test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )]
print ( "The original list is : " + str (test_list))
temp = list (chain( * test_list))
res = sum (temp) / len (test_list)
print ( "The mean of tuple list is : " + str (res))
|
Output
The original list is : [(1, 4, 5), (7, 8), (2, 4, 10)]
The mean of tuple list is : 13.666666666666666
Time complexity: O(n), where n is the total number of elements in the tuple list.
Auxiliary space: O(n), where n is the total number of elements in the tuple list.
Method #3: Using map() + sum()
This method uses the map() function to iterate through each tuple in the list and add its elements together, and then the sum() function to find the total sum of all elements in the tuples. Then, the mean is calculated by dividing the total sum by the number of tuples in the list.
Python3
test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )]
print ( "The original list is : " + str (test_list))
temp = sum ( map ( sum , test_list))
res = temp / len (test_list)
print ( "The mean of tuple list is : " + str (res))
|
Output
The original list is : [(1, 4, 5), (7, 8), (2, 4, 10)]
The mean of tuple list is : 13.666666666666666
Time complexity: O(n)
Auxiliary Space: O(n)
Method#4: Using List comprehension
Python3
test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )]
flattened_list = [i for sublist in test_list for i in sublist]
print ( "The original list is : " + str (test_list))
mean = sum (flattened_list) / len (test_list)
print ( "The mean of the tuple list is:" , mean)
|
Output
The original list is : [(1, 4, 5), (7, 8), (2, 4, 10)]
The mean of the tuple list is: 13.666666666666666
Time complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using numpy
Python3
import numpy as np
test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )]
print ( "The original list is : " + str (test_list))
arr = np.array(test_list)
result = np. sum (arr)
print ( "The mean of the tuple list is:" , sum (result) / len (test_list))
|
Output:
The original list is : [(1, 4, 5), (7, 8), (2, 4, 10)]
The mean of the tuple list is: 13.666666666666666
Time complexity: O(n)
Auxiliary Space: O(n)
Method#6: Using the Recursive method:
Python3
def mean_of_tuple_list(lst, n = 0 , s = 0 ):
if not lst:
return s / n
s + = sum (lst[ 0 ])
return mean_of_tuple_list(lst[ 1 :], n + 1 , s)
test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )]
print ( "The original list is : " + str (test_list))
res = mean_of_tuple_list(test_list)
print ( "The mean of tuple list is:" , res)
|
Output
The original list is : [(1, 4, 5), (7, 8), (2, 4, 10)]
The mean of tuple list is: 13.666666666666666
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7 : Using extend(), sum(), list(), len() methods:
Approach
- Convert the list of tuples to a single list using extend() and list() methods.
- Find the average and print the average using the sum() and len() methods.
Below is the implementation of the above approach:
Python3
test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )]
print ( "The original list is : " + str (test_list))
x = []
for i in test_list:
x.extend( list (i))
y = sum (x)
res = y / len (test_list)
print ( "The mean of tuple list is : " + str (res))
|
Output
The original list is : [(1, 4, 5), (7, 8), (2, 4, 10)]
The mean of tuple list is : 13.666666666666666
Time Complexity: O(N) N – length of extended list
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...