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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:06:34+00:00 2026-05-21T09:06:34+00:00

I tried writing some code like: i = [1, 2, 3, 5, 8, 13]

  • 0

I tried writing some code like:

i = [1, 2, 3, 5, 8, 13]
j = []
k = 0

for l in i:
    j[k] = l
    k += 1

But I get an error message that says IndexError: list assignment index out of range, referring to the j[k] = l line of code. Why does this occur? How can I fix it?

  • 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-21T09:06:35+00:00Added an answer on May 21, 2026 at 9:06 am

    j is an empty list, but you’re attempting to write to element [0] in the first iteration, which doesn’t exist yet.

    Try the following instead, to add a new element to the end of the list:

    for l in i:
        j.append(l)
    

    Of course, you’d never do this in practice if all you wanted to do was to copy an existing list. You’d just do:

    j = list(i)
    

    Alternatively, if you wanted to use the Python list like an array in other languages, then you could pre-create a list with its elements set to a null value (None in the example below), and later, overwrite the values in specific positions:

    i = [1, 2, 3, 5, 8, 13]
    j = [None] * len(i)
    #j == [None, None, None, None, None, None]
    k = 0
    
    for l in i:
       j[k] = l
       k += 1
    

    The thing to realise is that a list object will not allow you to assign a value to an index that doesn’t exist.

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

Sidebar

Related Questions

I am writing some code in VB.NET that uses a switch statement but in
In some code I am writing, sometimes I get ticks values like this Clear[z];
I'm writing some POSIX-specific code, and although I'm not supporting other platforms I'd like
I was writing some codes in linux using c. When tried to compiled, I
I am writing python script which gets links from website. But when I tried
I am writing some tests for Javascript code and I need to dump some
While writing some code i came across this issue: #include <iostream> class random {
I'm writing a program in C that needs to do some fast math calculations.
I am new to Python, and I'm working on writing some database code using
I'm writing some Javascript code for an ASP.net page. I have the string foo

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.