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

  • Home
  • SEARCH
  • 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 7655177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:26:48+00:00 2026-05-31T12:26:48+00:00

I have a django queryset that returns a list of values: [(client pk, timestamp,

  • 0

I have a django queryset that returns a list of values:

[(client pk, timestamp, value, task pk), (client pk, timestamp, value, task pk),....,].

I am trying to get it to return a dictionary of this format:

{client pk:[[timestamp, value],[timestamp, value],...,], client pk:[list of lists],...,}

The values_list may have multiple records for each client pk. I have been able to get dictionaries of lists for client or task pk using:

def dict_from_vl(vls_list):
    keys=[values_list[x][3] for x in range(0,len(values_list),1)]
    values = [[values_list[x][1], values_list[x][2]] for x in range(0,len(values_list),1)]
    target_dict=dict(zip(keys,values))
    return target_dict

However using this method, values for the same key write over previous values as it iterates through the values_list, rather than append them to a list. So this works great for getting the most recent if the values list is sorted oldest records to newest, but not for the purpose of creating a list of lists for the dict value.

  • 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-31T12:26:49+00:00Added an answer on May 31, 2026 at 12:26 pm

    Instead of target_dict=dict(zip(keys,values)), do

    target_dict = defaultdict(list)
    for i, key in enumerate(keys):
        target_dict[k].append(values[i])
    

    (defaultdict is available in the standard module collections.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a view that does a queryset and returns a simple list:
I have a list of objects from a django queryset, e.g. my_list = MyObject.objects.filter(variable=something)
I have a question about django content_types In the example of filtering a QuerySet
I was trying to setup django . I do have Django-1.1-alpha-1. I was trying
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I have a simple Django-Piston Handler that creates a new instance of a model
I have some django code that runs fine on a SQLite database or on
In my current django project I have a model that stores very long strings
I'm using django-tagging, and am trying to retrieve a list of tags for a
I have a fairly simple model that uses Django Taggit for tagging. Everything works

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.