Python | Selective Records Value Summation

Sometimes, while using list of tuples, we come across a problem in which we have certain list of keys and we just need the summation of values of those keys from list of tuples. This has a utility in rating or summation of specific entities. Let’s discuss certain ways in which this can be done.

Method #1 : Using dict() + sum() + get() + list comprehension
We can perform this particular task by first, converting the list into the dictionary and then employing list comprehension to get the value of specific keys using get function. The summation of values is performed using sum().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Selective Records Value Summation
# using dict() + get() + list comprehension + sum()
  
# initializing list of tuples 
test_list = [('Nikhil', 1), ('Akash', 2), ('Akshat', 3), ('Manjeet', 4)]
  
# initializing selection list 
select_list = ['Nikhil', 'Akshat']
  
# printing original list 
print ("The original list is : " + str(test_list))
  
# printing selection list 
print ("The selection list is : " + str(select_list))
  
# using dict() + get() + list comprehension + sum()
# Selective Records Value Summation
temp = dict(test_list)
res = sum([temp.get(i, 0) for i in select_list])
  
# printing result
print ("The selective values summation of keys : " + str(res))

chevron_right


Output :

The original list is : [('Nikhil', 1), ('Akash', 2), ('Akshat', 3), ('Manjeet', 4)]
The selection list is : ['Nikhil', 'Akshat']
The selective values summation of keys : 4

 

Method #2 : Using next() + sum() + list comprehension
This particular problem can be solved using the next function which performs the iteration using the iterators and hence more efficient way to achieve a possible solution. The summation of values is performed using sum().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Selective Records Value Summation
# using next() + list comprehension + sum()
  
# initializing list of tuples 
test_list = [('Nikhil', 1), ('Akash', 2), ('Akshat', 3), ('Manjeet', 4)]
  
# initializing selection list 
select_list = ['Nikhil', 'Akshat']
  
# printing original list 
print ("The original list is : " + str(test_list))
  
# printing selection list 
print ("The selection list is : " + str(select_list))
  
# using next() + list comprehension + sum()
# Selective Records Value Summation
res = sum([next((sub[1] for sub in test_list if sub[0] == i), 0) for i in select_list])
  
# printing result
print ("The selective values summation of keys : " + str(res))

chevron_right


Output :

The original list is : [('Nikhil', 1), ('Akash', 2), ('Akshat', 3), ('Manjeet', 4)]
The selection list is : ['Nikhil', 'Akshat']
The selective values summation of keys : 4



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.