Python | Summation of Kth Column of Tuple List

Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the possible accumulation of it’s Kth index. This problem has application in web development domain while working with data informations. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension + sum()
This task can be performed using the combination of above functionalities. In this, summation of index occurs using sum() and list comprehension drives the iteration and access of Nth index element of each tuple in list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Summation of Kth Column of Tuple List
# using list comprehension + sum()
  
# initialize list
test_list = [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initialize K
K = 2
  
# Summation of Kth Column of Tuple List
# using list comprehension + sum()
res = sum([sub[K] for sub in test_list])
  
# printing result
print("Summation of Kth Column of Tuple List : " + str(res))

chevron_right


Output :



The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Summation of Kth Column of Tuple List : 31

 

Method #2 : Using imap() + sum() + itemgetter()
The combination of above functions can also achieve this task. This approach is generator based and recommended in case we have a very large list. In this, sum() is used to perform summation, itemgetter to get Kth index and imap() performs the task of mapping elements to perform summation. Works only in Python2.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate working of
# Summation of Kth Column of Tuple List
# using imap() + sum() + itemgetter()
from operator import itemgetter
from itertools import imap
  
# initialize list
test_list = [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initialize K
K = 2
  
# Summation of Kth Column of Tuple List
# using imap() + sum() + itemgetter()
idx = itemgetter(K)
res = sum(imap(idx, test_list))
  
# printing result
print("Summation of Kth Column of Tuple List : " + str(res))

chevron_right


Output :

The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Summation of Kth Column of Tuple List : 31


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.