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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:21:11+00:00 2026-05-29T17:21:11+00:00

This is one of those embarassing pieces of code that simply needs a second

  • 0

This is one of those embarassing pieces of code that simply needs a second pair of eyes to spot the “obvious” mistake. I am processing a list of lists, and for some reason, I am hitting a ‘List Index out of range’ erorr – and I can’t spot it despite peering at the screen for a while.

Here is the snippet:

def group_ldata(data, freq, normal_grouping = True):
    if freq > 1 and len(data) > (SOME_SANITY_FACTOR*freq):
        i, output, subset, lastpos = (0, [],[], len(data)-freq)
        if not normal_grouping:
            for i in range(lastpos):
                pass
        else:
            while True:
                subset = data[i:freq]
                #print subset
                firstrow = subset[0]
                lastrow = subset[-1]
                output.append((firstrow[0], firstrow[1], lastrow[2]))
                i += freq
                if i >= lastpos:
                    break
        return output
    else:
        return data

Here’s some sample data to boot:

>>> a =[]
>>> a.append(range(0,5))
>>> a.append(range(5,10))
>>> a.append(range(10,15))
>>> a.append(range(15,20))
>>> a.append(range(20,25))
>>> a.append(range(25,30))
>>> a.append(range(30,35))

Here is the result when I run it:

>>> b = group_ldata(a,2)
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
[]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 11, in group_ldata
IndexError: list index out of range

Can anyone spot what is causing the error?

  • 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-29T17:21:13+00:00Added an answer on May 29, 2026 at 5:21 pm

    I am not sure what you are trying to do. But, the reason for the error is obvious.

    In the beginning, you do i=0.
    Then, later in the while True loop, you do i += freq. So, now i equals freq.

    Now, when the while True loop runs again (2nd iteration), in the line subset = data[i:freq], subset will be equal to a list with lenght 0. And you are trying to access the first element of that. Makes sense?

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

Sidebar

Related Questions

Hey, so this is one of those questions that seems obvious, and I'm probably
This is one of those things that you read once, say aha! and then
This is one of those FRUSTRATING bugs in that it works and then it
Ok this one is one of those that has had me bangin my head
I'm one of those people that has to get their code to compile with
This is one of those I probably should know this, but I don't questions.
This is just one of those little things I did all the time in
This is yet one more of those how to switch from running with a
Hey this is going to be one of those dumb questions. I am trying
I think this is going to be one of those simple-when-you-see-it problems, but it

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.