Python Forum
Newbie question for sum the specific element in named tuple - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Newbie question for sum the specific element in named tuple (/thread-6894.html)



Newbie question for sum the specific element in named tuple - zydjohn - Dec-12-2017

import collections
 
Person = collections.namedtuple('Person', ['Name', 'Age', 'Height', 'Weight'])
 
person1 = Person(Name='Bob', Age=55, Height=165, Weight=70)
person2 = Person(Name='Doe', Age=45, Height=185, Weight=90)
person3 = Person(Name='John', Age=50, Height=175, Weight=85)
people = [person1, person2, person3]
I have a list of namedtuple with people and their names, ages, heights and weights.
I want to know how to sum the people's total ages.
For my above example, the total ages = 55 + 45 + 50 = 150
Thanks,


RE: Newbie question for sum the specific element in named tuple - buran - Dec-12-2017

total_age = sum(person.Age for person in people)