Sometimes the data that we receive, is in the form of tuples having data from various sources and we can usually have a use case in which we require to process the finding average of each index of tuple for cumulation. Let’s discuss certain ways in which this can be performed.
Method #1: Using list comprehension This is the most naive method to perform this particular task, in this method we compute the average of each index of all the possible indices of the tuple.
Python3
# Python3 code to demonstrate # Column Average in Record List # using list comprehension # initializing list test_list = [( 1 , 6 ), ( 3 , 4 ), ( 5 , 8 )] # printing original list print ("The original list is : " + str (test_list)) # Column Average in Record List # using list comprehension temp = sum (i[ 0 ] for i in test_list), sum (i[ 1 ] for i in test_list) res = [] for ele in temp: res.append(ele / len (test_list)) # printing summation print ("The position Average of tuples : " + str (res)) |
The original list is : [(1, 6), (3, 4), (5, 8)] The position Average of tuples : [3.0, 6.0]
Time Complexity: O(n) where n is the number of elements in the string list. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.
Method #2: Using zip() + sum() This is the most elegant and pythonic way to perform this particular task. In this we combine all the indices of the element using zip() and the performance of summation using sum function. And then divide the list by list length to compute average.
Python3
# Python3 code to demonstrate # Column Average in Record List # using zip() + sum() # initializing list test_list = [( 1 , 6 ), ( 3 , 4 ), ( 5 , 8 )] # printing original list print ("The original list is : " + str (test_list)) # Column Average in Record List # using zip() + sum() temp = [ sum (i) for i in zip ( * test_list)] res = [] for ele in temp: res.append(ele / len (test_list)) # printing summation print ("The position Average of tuples : " + str (res)) |
The original list is : [(1, 6), (3, 4), (5, 8)] The position Average of tuples : [3.0, 6.0]
Method #3: Using statistics.mean()
Approach
- Separate the columns to different lists
- Find the average of each list using statistics.mean()
- Append average of each list to output list
- Display output list
Python3
# Python3 code to demonstrate # Column Average in Record List # initializing list test_list = [( 1 , 6 ), ( 3 , 4 ), ( 5 , 8 )] # printing original list print ( "The original list is : " + str (test_list)) # Column Average in Record List x = [] y = [] res = [] import statistics for i in range ( 0 , len (test_list)): x.append(test_list[i][ 0 ]) y.append(test_list[i][ 1 ]) res.append(statistics.mean(x)) res.append(statistics.mean(y)) # printing summation print ( "The position Average of tuples : " + str (res)) |
The original list is : [(1, 6), (3, 4), (5, 8)] The position Average of tuples : [3, 6]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #4: Using numpy.mean()
Step-by-Step Approach:
- Import numpy library using import numpy as np.
- Initialize the list of tuples test_list.
- Print the original list using print(“The original list is : ” + str(test_list)).
- Convert the list to a numpy array using arr = np.array(test_list).
- Calculate the column averages using res = np.mean(arr, axis=0). Here, axis=0 calculates the mean along the columns.
- Print the column averages using print(“The position Average of tuples : ” + str(res)).
Below is the implementation of the above approach:
Python
# import numpy import numpy as np # initializing list test_list = [( 1 , 6 ), ( 3 , 4 ), ( 5 , 8 )] # printing original list print ( "The original list is : " + str (test_list)) # Column Average in Record List using numpy.mean() arr = np.array(test_list) res = np.mean(arr, axis = 0 ) # printing column averages print ( "The position Average of tuples : " + str (res)) |
Output:
The original list is : [(1, 6), (3, 4), (5, 8)] The position Average of tuples : [3. 6.]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(n), where n is the number of tuples in the list (due to creating the numpy array).