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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:20:59+00:00 2026-05-16T01:20:59+00:00

I have a very long list of dictionaries with string indices and integer values.

  • 0

I have a very long list of dictionaries with string indices and integer values. Many of the keys are the same across the dictionaries, though not all. I want to generate one dictionary in which the keys are the union of the keys in the separate dictionaries and the values are the sum of all the values corresponding to that key in each of the dictionaries. (For example, the value for the key ‘apple’ in the combined dictionary will be the sum of the value of ‘apple’ in the first, plus the sum of the value of ‘apple’ in the second, etc.)

I have the following, but it’s rather cumbersome and takes ages to execute. Is there a simpler way to achieve the same result?

comb_dict = {}  
for dictionary in list_dictionaries:  
    for key in dictionary:  
        comb_dict.setdefault(key, 0)  
        comb_dict[key] += dictionary[key]  
return comb_dict
  • 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-05-16T01:21:00+00:00Added an answer on May 16, 2026 at 1:21 am

    Here are some microbenchmarks which suggest f2 (see below) might be an improvement. f2 uses iteritems which allows you avoid an extra dict lookup in the inner loop:

    import collections
    import string
    import random
    
    def random_dict():
        n=random.randint(1,26)
        keys=list(string.letters)
        random.shuffle(keys)
        keys=keys[:n]
        values=[random.randint(1,100) for _ in range(n)]    
        return dict(zip(keys,values))
    
    list_dictionaries=[random_dict() for x in xrange(100)]
    
    def f1(list_dictionaries):
        comb_dict = {}  
        for dictionary in list_dictionaries:  
            for key in dictionary:  
                comb_dict.setdefault(key, 0)  
                comb_dict[key] += dictionary[key]  
        return comb_dict
    
    def f2(list_dictionaries):    
        comb_dict = collections.defaultdict(int)
        for dictionary in list_dictionaries:  
            for key,value in dictionary.iteritems():  
                comb_dict[key] += value
        return comb_dict
    
    def union( dict_list ):
        all_keys = set()
        for d in dict_list:
            for k in d:
                all_keys.add( k )
        for key in all_keys:
            yield key, sum( d.get(key,0) for d in dict_list)
    
    def f3(list_dictionaries):
        return dict(union( list_dictionaries ))
    

    Here are the results:

    % python -mtimeit -s"import test" "test.f1(test.list_dictionaries)"
    1000 loops, best of 3: 776 usec per loop
    % python -mtimeit -s"import test" "test.f2(test.list_dictionaries)"
    1000 loops, best of 3: 432 usec per loop    
    % python -mtimeit -s"import test" "test.f3(test.list_dictionaries)"
    100 loops, best of 3: 2.19 msec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very long-running stored procedure in SQL Server 2005 that I'm trying
In a C# application I am working on I have a very long identifier
I have a SQL query that takes a very long time to run on
We have very strange problem, one of our applications is continually querying server by
I have very little experience building software for Windows, and zero experience using the
i have very simple problem. I need to create model, that represent element of
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have a very strange problem. Under some elusive circumstances I fail to apply
I have a very simple problem which requires a very quick and simple solution
I have a very simple WPF application in which I am using data binding

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.