Python Forum

Full Version: Newbie question for sum the specific element in named tuple
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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,
total_age = sum(person.Age for person in people)