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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:38:11+00:00 2026-06-12T04:38:11+00:00

I am trying to append a lengthy list of rows to the same variable.

  • 0

I am trying to append a lengthy list of rows to the same variable. It works great for the first thousand or so iterations in the loop (all of which have the same lengths), but then, near the end of the file, the rows get a bit shorter, and while I still want to append them, I am not sure how to handle it.

The script gives me an out of range error, as expected.

Here is what the part of code in question looks like:

    ii = 0
    NNCat = []
    NNCatelogue = []
    while ii <= len(lines):

        NNCat = (ev_id[ii], nn1[ii], nn2[ii], nn3[ii], nn4[ii], nn5[ii], nn6[ii], nn7[ii], nn8[ii], nn9[ii], nn10[ii], nn11[ii])
        NNCatelogue.append(NNCat)
        ii = ii + 1



    print NNCatelogue, ii

Any help on this would be greatly appreciated!

  • 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-12T04:38:12+00:00Added an answer on June 12, 2026 at 4:38 am

    I’ll answer the question you didn’t ask first 😉 : how can this code be more pythonic?

    Instead of

    ii = 0
    NNCat = []
    NNCatelogue = []
    while ii <= len(lines):
    
        NNCat = (ev_id[ii], nn1[ii], nn2[ii], nn3[ii], nn4[ii], nn5[ii], nn6[ii], nn7[ii], nn8[ii], nn9[ii], nn10[ii], nn11[ii])
        NNCatelogue.append(NNCat)
        ii = ii + 1
    

    you should do

    NNCat = []
    NNCatelogue = []
    for ii, line in enumerate(lines):
    
        NNCat = (ev_id[ii], nn1[ii], nn2[ii], nn3[ii], nn4[ii], nn5[ii], nn6[ii],
                 nn7[ii], nn8[ii], nn9[ii], nn10[ii], nn11[ii])
        NNCatelogue.append(NNCat)
    

    During each pass ii will be incremented by one for you and line will be the current line.

    As for your short lines, you have two choices

    • Use a special value (such as None) to fill in when you don’t have a real value
    • check the length of nn1, nn2, …, nn11 to see if they are large enough

    The second solution will be much more verbose, hard to maintain, and confusing. I strongly recommend using None (or another special value you create yourself) as a placeholder when there is no data.

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

Sidebar

Related Questions

I'm trying to use this example to create a program which will list all
I'm trying to loop all matched results so they appear in a list form.
I am trying to append a li element to a list with a link
I'm trying to append some text to a variable in a Makefile for HP-UX's
I am trying to create a Python dictionary from a stored list. This first
I am trying append some XML retrieved via a dojo.XHRGet to a dijit.layout.ContentPane .
I am trying to append some text to excel cell. For example if the
I'm trying to append a CSS class to a row on RowDataBound. I'm using
I am trying to append to a log file, using HTML tables format for
I'm trying to append a string of svg elements to the dom. This is

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.