Open In App
Related Articles

Python – Kth Index Tuple List Mean

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# Python3 code to demonstrate working of
# Kth Index Tuple List Mean
# Using mean() + generator expression
from statistics import mean
 
# initializing list
test_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 1)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# Kth Index Tuple List Mean
# Using mean() + generator expression
res = mean(val[K] for val in test_list)
   
# printing result
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

# Python3 code to demonstrate working of
# Kth Index Tuple List Mean
# Using sum() + len() + generator expression
from statistics import mean
 
# initializing list
test_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 1)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# Kth Index Tuple List Mean
# Using sum() + len() + generator expression
res = sum(val[K] for val in test_list) / len(test_list)
   
# printing result
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

# import the reduce() function from the functools module
from functools import reduce
 
# initializing list
test_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 1)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 1
 
# Using the reduce() function to compute the sum and then divide by the length of the list
res = reduce(lambda x, y: x + y[K], test_list, 0) / len(test_list)
 
# printing result
print("The computed mean : " + str(res))
#This code is contributed By VInay Pinjala.

                    

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

# Python3 code to demonstrate working of
# Kth Index Tuple List Mean
# Using a loop
 
# initializing list
test_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 1)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# Kth Index Tuple List Mean
# Using a loop
total = 0
count = 0
for val in test_list:
    total += val[K]
    count += 1
res = total / count
 
# printing result
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()

  1. Import the numpy library using the command
  2. Create the list of tuples as mentioned in the problem statement
  3. Extract the Kth index of all tuples and create a list
  4. Use the numpy.mean() function to calculate the mean of the kth_index_list
  5. Print the result

Python3

import numpy as np
 
# initializing list
test_list = [('Gfg', 4), ('is', 18), ('best', 2), ('for', 5), ('geeks', 1)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 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), ('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)



Last Updated : 16 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads