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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:00:35+00:00 2026-05-30T06:00:35+00:00

I have to merge list of python dictionary. For eg: dicts[0] = {‘a’:1, ‘b’:2,

  • 0

I have to merge list of python dictionary. For eg:

dicts[0] = {'a':1, 'b':2, 'c':3}
dicts[1] = {'a':1, 'd':2, 'c':'foo'}
dicts[2] = {'e':57,'c':3}

super_dict = {'a':[1], 'b':[2], 'c':[3,'foo'], 'd':[2], 'e':[57]}    

I wrote the following code:

super_dict = {}
for d in dicts:
    for k, v in d.items():
        if super_dict.get(k) is None:
            super_dict[k] = []
        if v not in super_dict.get(k):
            super_dict[k].append(v)

Can it be presented more elegantly / optimized?

Note
I found another question on SO but its about merging exactly 2 dictionaries.

  • 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-30T06:00:36+00:00Added an answer on May 30, 2026 at 6:00 am

    You can iterate over the dictionaries directly — no need to use range. The setdefault method of dict looks up a key, and returns the value if found. If not found, it returns a default, and also assigns that default to the key.

    super_dict = {}
    for d in dicts:
        for k, v in d.iteritems():  # d.items() in Python 3+
            super_dict.setdefault(k, []).append(v)
    

    Also, you might consider using a defaultdict. This just automates setdefault by calling a function to return a default value when a key isn’t found.

    import collections
    super_dict = collections.defaultdict(list)
    for d in dicts:
        for k, v in d.iteritems():  # d.items() in Python 3+
            super_dict[k].append(v)
    

    Also, as Sven Marnach astutely observed, you seem to want no duplication of values in your lists. In that case, set gets you what you want:

    import collections
    super_dict = collections.defaultdict(set)
    for d in dicts:
        for k, v in d.iteritems():  # d.items() in Python 3+
            super_dict[k].add(v)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
I have two objects and I want to merge them: public class Foo {
I have a list of dicts and I want to compare each dict in
I am trying to merge two list in parallel. I have two sorted lists
I have a list of List<KeyvaluePair<DateTime, int>> , which I want to merge into
In Python I have four strings that include the formatting of a list: line1
I have 8 sorted lists that I need to merge into 1 sorted list.
I have a list of arrays (unknown amount), I need to merge all of
I have a List of Map[String, Double], and I'd like to merge their contents
I have a big list, but micro example would be like the following: A

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.