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
  Searche each element of each tuple based 3 numbes zinho 8 856 Dec-11-2023, 05:14 PM
Last Post: zinho
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 691 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 676 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 594 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 967 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  numpy newbie question bcwilly_ca 4 1,177 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  search a list or tuple for a specific type ot class Skaperen 8 1,918 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  convert a named tuple to a dictionary Skaperen 13 3,483 Mar-31-2022, 07:13 PM
Last Post: Skaperen
  myList.insert(index, element) question ChrisF 1 1,641 Aug-27-2021, 03:49 PM
Last Post: bowlofred
  Question from complete python's newbie Davicom 3 2,364 Jun-09-2021, 06:09 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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