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 8386251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:53:03+00:00 2026-06-09T17:53:03+00:00

I have a dict, d = {‘a’: [4,’Adam’, 2], ‘b’: [3,’John’, 4], ‘c’: [4,’Adam’,

  • 0

I have a dict,

d = {'a': [4,'Adam', 2], 'b': [3,'John', 4], 'c': [4,'Adam', 3], 'd': [4,'Bill', 3], 'e': [4,'Bob'], 'f': [4, 'Joe'], 'g': [4, 'Bill']}

Is there any quick way to get a sum of the numbers in each of the lists in the dictionary?

For example, a should return 6, b should return 7, so on.

Currently, I am doing this.

for i in d:
    l2=[]
    for thing in d[i]:
        if type(thing) == int:
            l2.append(thing)
        print sum(l2)

Possible for a quicker fix than having to go through each time and append the numbers to a list?

Thanks!

  • 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-09T17:53:05+00:00Added an answer on June 9, 2026 at 5:53 pm

    Here is a fairly straight forward way using a dictionary comprehension:

    sums = {k: sum(i for i in v if isinstance(i, int)) for k, v in d.items()}
    

    Or on Python 2.6 and below:

    sums = dict((k, sum(i for i in v if isinstance(i, int))) for k, v in d.items())
    

    Example:

    >>> {k: sum(i for i in v if isinstance(i, int)) for k, v in d.items()}
    {'a': 6, 'c': 7, 'b': 7, 'e': 4, 'd': 7, 'g': 4, 'f': 4}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way to implement a paging solution using dict than this?
In a Django template, is there a way to get a value from a
I have a dict where I want to say if there is a category,
i have a dict d1={'a':['in-gaap','inr',0,1],'b':['in-gaap','inr',0,2],'c':['in-ca','share',0,4],'n1':['','','','aaa']} d2={'d':['in-gaap','inr',0,'a+b'],'e':['in-gaap','inr',0,'y+t']} for k in d2.iterkeys(): a=re.findall('\w+',d2[k][3]) x2=dict([(x,d1.get(x,0)[3])for x in
Have dict like: mydict= {'a':[],'b':[],'c':[],'d':[]} list like: log = [['a',917],['b', 312],['c',303],['d',212],['a',215],['b',212].['c',213],['d',202]] How do i
My issue is as follows. We have dict that has all keys and values
I have a dict object. I dumped the data using this: for alldata in
I have a dict which contains some lists and some dicts, as illustrated below.
I have a default dict of dicts whose primary key is a timestamp in
I have a list of dict convert from yaml, but how do I merge

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.