Python Forum
Newbie question for sum the specific element in named tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question for sum the specific element in named tuple
#1
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,
Reply
#2
total_age = sum(person.Age for person in people)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question from complete python's newbie Davicom 3 212 Jun-09-2021, 06:09 PM
Last Post: bowlofred
  Newbie question about running Python via GUI on OSX ejwjohn 8 662 Feb-05-2021, 03:20 PM
Last Post: Larz60+
  super newbie question: escape character tsavoSG 3 442 Jan-13-2021, 04:31 AM
Last Post: tsavoSG
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 637 Nov-04-2020, 11:26 AM
Last Post: Aggam
  finding element of specific field in pandas adjacency matrix amjass12 0 337 Oct-27-2020, 09:27 AM
Last Post: amjass12
  newbie question....importing a created class ridgerunnersjw 5 643 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Dumb newbie question JonEdward 5 871 Jul-22-2020, 10:06 PM
Last Post: snippsat
  how to get the keys in a named tuple Skaperen 5 1,619 Mar-25-2020, 09:51 PM
Last Post: Skaperen
  Newbie Question juljan 4 961 Jan-13-2020, 03:07 AM
Last Post: snippsat
  Newbie question... mrochac 2 707 Nov-14-2019, 02:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020