Python – Kth Index Tuple List Mean

Sometimes, while working with Python tuple, we can have a problem in which we need to compute average of any particular index of tuples in a list. This kind of problem can have application in data domain such as web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [(‘Gfg’, 1), (‘is’, 5), (‘best’, 7)], K = 1
Output : 4.333333333333333Input : test_list = [(‘Gfg’, 7), (‘best’, 7)], K = 1
Output : 7
Method #1: Using mean() + generator expression
The combination of above functions can be used to solve this problem. In this, we perform the task of mean computation using mean() and generator expression is used for iterations.
Python3
# Python3 code to demonstrate working of # Kth Index Tuple List Mean# Using mean() + generator expressionfrom statistics import mean# initializing listtest_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 1# Kth Index Tuple List Mean# Using mean() + generator expressionres = mean(val[K] for val in test_list) # printing result print("The computed mean : " + str(res)) |
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]
The computed mean : 6
Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(1), the space used is constant irrespective of the input size.
Method #2 : Using sum() + len() + generator expression
The combination of above functions can also be employed to solve this task. In this, we perform task of summation computation using sum() and result is divided by list length computed using len().
Python3
# Python3 code to demonstrate working of # Kth Index Tuple List Mean# Using sum() + len() + generator expressionfrom statistics import mean# initializing listtest_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 1# Kth Index Tuple List Mean# Using sum() + len() + generator expressionres = sum(val[K] for val in test_list) / len(test_list) # printing result print("The computed mean : " + str(res)) |
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]
The computed mean : 6.0
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1) here constant space is required
Method #3: Using reduce
This code uses the reduce() function from the functools module to compute the sum of the second element of each tuple in the list. It then divides the sum by the length of the list to compute the mean of the second element of the tuples in the list.
Python3
# import the reduce() function from the functools modulefrom functools import reduce# initializing listtest_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 1# Using the reduce() function to compute the sum and then divide by the length of the listres = reduce(lambda x, y: x + y[K], test_list, 0) / len(test_list)# printing resultprint("The computed mean : " + str(res)) #This code is contributed By VInay Pinjala. |
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]
The computed mean : 6.0
Time complexity: O(n), where n is the length of the input list. This is because the reduce() function iterates through the list once to compute the sum of the second element of the tuples, and then the final division operation takes constant time.
Auxiliary space: O(1), because it only uses a constant amount of extra space to store the sum and the length of the list.
Method #4: Using a loop
Step-by-step approach:
- Initialize two variables total and count to zero.
- Use a loop to iterate over each tuple in test_list.
- Add the value at the K index of each tuple to total.
- Increment count by one for each tuple.
- Compute the mean by dividing total by count.
- Print the computed mean.
Python3
# Python3 code to demonstrate working of # Kth Index Tuple List Mean# Using a loop# initializing listtest_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 1# Kth Index Tuple List Mean# Using a looptotal = 0count = 0for val in test_list: total += val[K] count += 1res = total / count# printing result print("The computed mean : " + str(res)) |
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]
The computed mean : 6.0
Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(1).
Method #5: Using numpy.mean()
- Import the numpy library using the command
- Create the list of tuples as mentioned in the problem statement
- Extract the Kth index of all tuples and create a list
- Use the numpy.mean() function to calculate the mean of the kth_index_list
- Print the result
Python3
import numpy as np# initializing listtest_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 1# Kth Index Tuple List Mean# Using numpy.mean()kth_index_list = [tup[K] for tup in test_list]res = np.mean(kth_index_list)# printing result print("The computed mean : " + str(res)) |
Output:
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('zambiatek', 1)]
The computed mean : 6.0
Time complexity: O(n), where n is the length of the input list
Auxiliary space: O(n), where n is the length of the input list (to store the kth index list)



