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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:26:22+00:00 2026-06-02T13:26:22+00:00

I want to filter a for loop over a groupby filter based on the

  • 0

I want to filter a for loop over a groupby filter based on the loop variable. This is what I’m doing:

{% for group in list_of_dicts | groupby('attribute') -%}
    {% if loop.index < 9 %}
    ...
    {% endif %}
{% endfor %}

It works as I expect. In the docs there is this syntax:

{% for user in users if not user.hidden %}
    <li>{{ user.username|e }}</li>
{% endfor %}

How to use the above mentioned syntax when looping over a filter? I mean like the following, which raises an UndefinedError:

{% for group in list_of_dicts | groupby('attribute') if loop.index < 9 -%}
    ...
{% endfor %}

UndefinedError: 'loop' is undefined. the filter section of a loop as well as the else block don't have access to the special 'loop' variable of the current loop.  Because there is no parent loop it's undefined.  Happened in loop on line 18 in 'page.html'
  • 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-02T13:26:26+00:00Added an answer on June 2, 2026 at 1:26 pm

    The filter works like in a normal Python LC (You can just access group).

    Using a filter in this case, doesn’t make sense anyways. E.g. the grouped list_of_dicts contains let’s say, 3000 elements, so you’re doing 3000 iterations but you just want 9. You should slice your groups:

    {% for group in (list_of_dicts | groupby('attribute'))[:9] -%}
        ...
    {% endfor %}
    

    (Assuming the filter returns a list)

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

Sidebar

Related Questions

I want to filter based on an attribute called level. Where I have written
I want to filter a java.util.Collection based on a predicate.
I want to filter rows from a data.frame based on a logical condition. Let's
I want to filter based on two attributes, the first is id and the
I want to filter elements from a list of lists, and iterate over the
I want to filter a table using jQuery hide/show based on what the user
I want to loop over the elements of an HTML form, and store the
I searched all over for a way to do this. I want to open
I have a big ArrayCollection variable. I want to filter it several times and
Say you want to filter elements out of a List. Running this code triggers

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.