Python | Mean of tuple list
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
# Python3 code to demonstrate working of # Mean of tuple list # Using loops # Initializing list test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) # Average of tuple list # Using loops sum = 0 for sub in test_list: for i in sub: sum = sum + i res = sum / len (test_list) # printing result print ( "The mean of tuple list is : " + str (res)) |
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
# Python3 code to demonstrate working of # Mean of tuple list # Using chain() + sum() from itertools import chain # Initializing list test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) # Average of tuple list # Using chain() + sum() temp = list (chain( * test_list)) res = sum (temp) / len (test_list) # printing result print ( "The mean of tuple list is : " + str (res)) |
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
# Python3 code to demonstrate working of # Mean of tuple list # Using map() + sum() # Initializing list test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) # Average of tuple list # Using map() + sum() temp = sum ( map ( sum , test_list)) res = temp / len (test_list) # printing result print ( "The mean of tuple list is : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
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] # printing original list print ( "The original list is : " + str (test_list)) mean = sum (flattened_list) / len (test_list) print ( "The mean of the tuple list is:" , mean) #This code is contributed by Jyothi Pinjala. |
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 # Driver Code test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) # Convert the list of tuples to a 2D numpy array arr = np.array(test_list) # Sum the elements along the rows to get the sum of each tuple result = np. sum (arr) print ( "The mean of the tuple list is:" , sum (result) / len (test_list)) # This code is contributed Vinay pinjala. |
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
# Python3 code to demonstrate working of # Mean of tuple list # Using recursive method 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) # Initializing list test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) res = mean_of_tuple_list(test_list) #printing result print ( "The mean of tuple list is:" , res) #this code contributed by tvsk |
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)
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
# Python3 code to demonstrate working of # Mean of tuple list using loops # Initializing list test_list = [( 1 , 4 , 5 ), ( 7 , 8 ), ( 2 , 4 , 10 )] # printing original list print ( "The original list is : " + str (test_list)) # Average of tuple list using loops x = [] for i in test_list: x.extend( list (i)) y = sum (x) res = y / len (test_list) # printing result print ( "The mean of tuple list is : " + str (res)) |
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)
Contact Us