Python | Summation of list as tuple attribute
Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the summation of list as tuple attribute. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + sum()
This particular problem can be solved using list comprehension combined with the sum function in which we use sum function to find the sum of list as a tuple attribute and list comprehension to iterate through the list.
# Python3 code to demonstrate # Summation of list as tuple attribute # using list comprehension + sum() # initializing list test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] # printing original list print("The original list : " + str(test_list)) # using list comprehension + sum() # Summation of list as tuple attribute res = [(key, sum(lst)) for key, lst in test_list] # print result print("The list tuple attribute summation is : " + str(res)) |
The original list : [(‘key1’, [3, 4, 5]), (‘key2’, [1, 4, 2]), (‘key3’, [9, 3])]
The list tuple attribute summation is : [(‘key1’, 12), (‘key2’, 7), (‘key3’, 12)]
Method #2 : Using map + lambda + sum()
The above problem can also be solved using the map function to extend the logic to the whole list and sum function can perform the similar task as the above method.
# Python3 code to demonstrate # Summation of list as tuple attribute # using map() + lambda + sum() # initializing list test_list = [('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])] # printing original list print("The original list : " + str(test_list)) # using map() + lambda + sum() # Summation of list as tuple attribute res = list(map(lambda x: (x[0], sum(x[1])), test_list)) # print result print("The list tuple attribute summation is : " + str(res)) |
The original list : [(‘key1’, [3, 4, 5]), (‘key2’, [1, 4, 2]), (‘key3’, [9, 3])]
The list tuple attribute summation is : [(‘key1’, 12), (‘key2’, 7), (‘key3’, 12)]
Recommended Posts:
- Python | Summation of Kth Column of Tuple List
- Python | Grouped summation of tuple list
- Python | Sort tuple list by Nth element of tuple
- Python | Summation of tuples in list
- Python | Summation of dictionary list values
- Python | Alternate element summation in list
- Python | Pair summation of list elements
- Python | Summation of integers in heterogenous list
- Python | Position Summation in List of Tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Pair and combine nested list to tuple list
- Python | Convert Integral list to tuple list
- Python | Convert list to indexed tuple list
- Python | Mean of tuple list
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.



