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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:34:23+00:00 2026-06-17T18:34:23+00:00

I have written a django query like this. stud_record = Student.objects.filter() Thus stud_record is

  • 0

I have written a django query like this.

stud_record = Student.objects.filter()

Thus stud_record is a queryset.

Now what I want to do is, i want to calculate the total num of stud_record, and then divide it by 5.

For ex: I have 20 records in stud_record. After dividing by 5 I got 4 each. So now i want to rank the first four students as ran 1, then from 5-8 students as rank 2….16-20 students as rank 5. This rank will be inserted to the same stud_record and will be send to the template page.

How can we do it.

Currently I am getting the structure like this

[{'name': u'mark', 'roll': 71}, 
 {'name': u'robin', 'roll': 42}, 
 {'name': u'julien', 'roll': 39}]

After division and inserting the rank it should look like

[[{'name': u'mark', 'roll': 71, 'rank': 1}, 
  {'name': u'robin', 'roll': 42, 'rank': 1},] 
 [{'name': u'julien', 'roll': 39, 'rank': 2}]]

Right now I am getting 185 results in my stud_record. After dividing 185/5 i am getting 37 as result. Now i want to give rank 1 to 1-37 students, then rank 2 to 38-75 students… like this

  • 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-17T18:34:25+00:00Added an answer on June 17, 2026 at 6:34 pm

    Add ranks to your dicts first and then group them using itertools.groupby:

    from itertools import groupby
    
    lst = [{} for _ in range(21)]  # simple testlist
    
    def rank_it(lst,n):
        for index,d in enumerate(lst):
            rank = index/(len(lst)/n)+1
            d['rank'] = rank if rank < n else n
    
    rank_it(lst,5)  #  add ranks
    [list(g) for _,g in groupby(lst, lambda x: x['rank'])]  #group by rank
    

    out:

    [[{'rank': 1}, {'rank': 1}, {'rank': 1}, {'rank': 1}],
     [{'rank': 2}, {'rank': 2}, {'rank': 2}, {'rank': 2}],
     [{'rank': 3}, {'rank': 3}, {'rank': 3}, {'rank': 3}],
     [{'rank': 4}, {'rank': 4}, {'rank': 4}, {'rank': 4}],
     [{'rank': 5}, {'rank': 5}, {'rank': 5}, {'rank': 5}, {'rank': 5}]]
    

    NB! This solution needs the list to be sorted, if its not, add:
    lst.sort(key=lambda x: x['roll'],reverse=True)

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

Sidebar

Related Questions

I have the view function in django that written like a dispatcher calling other
I've written an entire app pretty successfully in Django but I have this nagging
I have written a django page that requires only super users to login. So
I have written a custom Django Model Field to store dateutil.relativedelta 's. The field
I had the following idea: Say we have a webapp written using django which
I have used httpclient to call a restapi written in django. It returned the
I have a Django app written in Python 2.5 and I plan to upgrade
I have some unit tests I've written to test my Django application. One test
I have two servers - one Django, the other likely to be written in
django guardian https://github.com/lukaszb/django-guardian is a really well written object-level permissions app; and I have

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.