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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:12:26+00:00 2026-05-18T07:12:26+00:00

I have a list of objects : film_hc = [{‘count’: 2, ‘pk’: ’33’}, {‘count’:

  • 0

I have a list of objects :

film_hc = [{'count': 2, 'pk': '33'},
           {'count': 1, 'pk': '37'},
           {'count': 1, 'pk': '49'}]

The ‘pk’ value is the primary key for a record in a model. I would like to add the name field of that record to this list of objects. To get one name, I can use:

record = Film.objects.get(pk = film_hc[0]['pk'])  
record.name

In the end, I would like to have something like this:

film_hc = [{'count': 2, 'pk': '33', 'name': 'name1'},
           {'count': 1, 'pk': '37', 'name': 'name2'},
           {'count': 1, 'pk': '49', 'name': 'name3'}]

Question: What is the most efficient way to attach the necessary data to this preexisting list?

I am thinking I could use the zip function:

film_hc_with_names = zip(????, film_hc)

The problem is I’m not sure what I would substitute in place of those ???? to get the object then the name for each object in the list. Should I use a for loop instead? What is the most preferable option?

  • 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-18T07:12:27+00:00Added an answer on May 18, 2026 at 7:12 am

    To avoid hitting the database multiple times, I recommend you use the in_bulk queryset method. This takes a list of IDs, and returns a dictionary of ID mapped to model instance. So what you need to do is to run through your list of dictionaries first to extract the ID values, then do the query, then run through again to get the name for each instance. Even though this is doing two extra iterations, it should still be quicker than running multiple DB queries (although as always you should profile to make sure).

    id_list = [film['id'] for film in film_hc]
    objects = Film.objects.only('name').in_bulk(id_list)
    for film in film_hc:
        film['name'] = objects[film['id']].name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of objects (mainly int, string, double) And I would like
I have a list of custom objects List and I would like to update
I have a list of Child objects. I would like to be able to
I have a list of objects and I would like to access the objects
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have a list of objects and I want to change the value of
I have a List of objects and I'd like to loop over that list
I have a list of objects.Every object has an attribute priority whose value could
I have a list of objects in python that I would regularly check and
I have a list of Objects (the model) that are constantly appended to (similar

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.