Hacker News new | ask | show | jobs
by jeremiahwv 2963 days ago
For simple cases like this it doesn't even cost a couple lines as sum() can take a generator expression:

  sum(planet['moons'] for planet in target['system']['planets'])
1 comments

Combining ideas from parent and grandparent:

  sum(planet.get('moons', 0) for planet in target['system']['planets'])