Python – Kth Index Tuple List Mean
Last Updated :
16 May, 2023
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.333333333333333
Input : 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
from statistics import mean
test_list = [( 'Gfg' , 4 ), ( 'is' , 18 ), ( 'best' , 2 ), ( 'for' , 5 ), ( 'geeks' , 1 )]
print ( "The original list is : " + str (test_list))
K = 1
res = mean(val[K] for val in test_list)
print ( "The computed mean : " + str (res))
|
Output :
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 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
from statistics import mean
test_list = [( 'Gfg' , 4 ), ( 'is' , 18 ), ( 'best' , 2 ), ( 'for' , 5 ), ( 'geeks' , 1 )]
print ( "The original list is : " + str (test_list))
K = 1
res = sum (val[K] for val in test_list) / len (test_list)
print ( "The computed mean : " + str (res))
|
Output :
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 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
from functools import reduce
test_list = [( 'Gfg' , 4 ), ( 'is' , 18 ), ( 'best' , 2 ), ( 'for' , 5 ), ( 'geeks' , 1 )]
print ( "The original list is : " + str (test_list))
K = 1
res = reduce ( lambda x, y: x + y[K], test_list, 0 ) / len (test_list)
print ( "The computed mean : " + str (res))
|
Output
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 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
test_list = [( 'Gfg' , 4 ), ( 'is' , 18 ), ( 'best' , 2 ), ( 'for' , 5 ), ( 'geeks' , 1 )]
print ( "The original list is : " + str (test_list))
K = 1
total = 0
count = 0
for val in test_list:
total + = val[K]
count + = 1
res = total / count
print ( "The computed mean : " + str (res))
|
Output
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 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
test_list = [( 'Gfg' , 4 ), ( 'is' , 18 ), ( 'best' , 2 ), ( 'for' , 5 ), ( 'geeks' , 1 )]
print ( "The original list is : " + str (test_list))
K = 1
kth_index_list = [tup[K] for tup in test_list]
res = np.mean(kth_index_list)
print ( "The computed mean : " + str (res))
|
Output:
The original list is : [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...