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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:37:34+00:00 2026-06-16T06:37:34+00:00

I have two lists of dictionaries (returned as Django querysets). Each dictionary has an

  • 0

I have two lists of dictionaries (returned as Django querysets). Each dictionary has an ID value. I’d like to merge the two into a single list of dictionaries, based on the ID value.

For example:

list_a = [{'user__name': u'Joe', 'user__id': 1},
          {'user__name': u'Bob', 'user__id': 3}]
list_b = [{'hours_worked': 25, 'user__id': 3},
          {'hours_worked': 40, 'user__id': 1}]

and I want a function to yield:

list_c = [{'user__name': u'Joe', 'user__id': 1, 'hours_worked': 40},
          {'user__name': u'Bob', 'user__id': 3, 'hours_worked': 25}]

Additional points to note:

  • The IDs in the lists may not be in the same order (as with the example above).
  • The lists will probably have the same number of elements, but I want to account for the option if they’re not but keeping all the values from list_a (essentially list_a OUTER JOIN list_b USING user__id).
  • I’ve tried doing this in SQL but it’s not possible since some of the values are aggregates based on some exclusions.
  • It’s safe to assume there will only be at most one dictionary with the same user__id in each list due to the database queries used.

Many thanks for your time.

  • 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-16T06:37:35+00:00Added an answer on June 16, 2026 at 6:37 am

    I’d use itertools.groupby to group the elements:

    lst = sorted(itertools.chain(list_a,list_b), key=lambda x:x['user__id'])
    list_c = []
    for k,v in itertools.groupby(lst, key=lambda x:x['user__id']):
        d = {}
        for dct in v:
            d.update(dct)
        list_c.append(d)
        #could also do:
        #list_c.append( dict(itertools.chain.from_iterable(dct.items() for dct in v)) )
        #although that might be a little harder to read.
    

    If you have an aversion to lambda functions, you can always use operator.itemgetter('user__id') instead. (it’s probably slightly more efficient too)

    To demystify lambda/itemgetter a little bit, Note that:

    def foo(x):
        return x['user__id']
    

    is the same thing* as either of the following:

    foo = operator.itemgetter('user__id')
    foo = lambda x: x['user__id']
    

    *There are a few differences, but they’re not important for this problem

    • 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 Lists of dictionaries. Both are not not null. How to merge
I have constructed two dictionary that contains histogram values of images. Each dictionary has
I have two dictionaries. When I change a value in dictionary 1, the same
I have two Lists, they look like this <List> ads [0] Headline = Sony
I have two complex dictionaries in the form Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> So as
I have two lists: a = [(1,2,3),(4,5,6)] b = [7,8] I want to merge
I have two lists List<WorkOrder> and List<PlannedWork> I would like join the two lists
I have two lists of dictionaries list1 = [ {..}, {..}, ..] list2 =
I have two lists that i need to combine where the second list has

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.