Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8245171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:05:39+00:00 2026-06-07T22:05:39+00:00

I’m trying to define a function that will allow me to get a value

  • 0

I’m trying to define a function that will allow me to get a value count at various levels of a dict. For example, if I have something like:

vehicles = {
    'Ford': {
        'Falcon': {
            '2008': 10, 
            '2009': 12, 
            '2010': 5
        }
    }, 
    'Holden': {
        'Commodore': {
            '2008': 15, 
            '2009': 11, 
            '2010': 5
        }
    }
}

I’d like to be able to call a function that will calculate the values at each depth. So at depth 0, the function would return the sum of every value (58). At depth 1, the function would return a dict with {'Ford': 27, 'Holden': 31}. At depth 2, the function would return a dict with {'Ford': {'Falcon': 27}, 'Holden': {'Commodore': 31}}. Etc…

I think I need to burrow to the max depth and start summing the values as I move back up the levels, but I’m fairly new to Python and I’m struggling. Possibly I would need to use recursion?

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:05:41+00:00Added an answer on June 7, 2026 at 10:05 pm

    My solution, not thoroughly tested

    
    def sum_values(data):
        if type(data.values()[0]) != dict:
            return sum(data.values())
        else:
            return sum(sum_values(v) for v in data.values())
    
    def depth_values(data, depth = 0):
        if depth == 0:
            return sum_values(data)
        else:
            return dict(
                (k, depth_values(v, depth-1))
                for k, v in data.items()
            )
    

    TEST:

    
    print depth_values(vehicles, 0) # >> 58
    print depth_values(vehicles, 1) # >> {'Holden': 31, 'Ford': 27}
    print depth_values(vehicles, 2) # >> {'Holden': {'Commodore': 31}, 'Ford': {'Falcon': 27}}
    print depth_values(vehicles, 3) # exceptions
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.