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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:01:30+00:00 2026-05-26T20:01:30+00:00

I am creating a list, and then accessing an element like this: list =

  • 0

I am creating a list, and then accessing an element like this:

list = []
list.insert(42, "foo") 
list.insert(43, "bar") 
list.insert(44, "baz")

print(list[43])

And I have the folowing error:

print(list[43]) IndexError: list index out of range

What is wrong ? Do I have to use a dictionary to do 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-05-26T20:01:30+00:00Added an answer on May 26, 2026 at 8:01 pm

    There are no “keys” in a list, there are just indices.

    The reason your code doesn’t work the way you expect is that list.insert(index, obj) does not pad the list with “blank” entries when index is past the end of the list; it simply appends obj to the list.

    You could use a dictionary for this:

    In [14]: d = {}
    In [15]: d[42] = "foo"
    In [16]: d[43] = "bar"
    In [17]: d[44] = "baz"
    In [18]: print(d[43])
    bar
    

    Alternatively, you could pre-initialize your list with a sufficiently large number of entries:

    In [19]: l = [None] * 50
    In [20]: l[42] = "foo"
    In [21]: l[43] = "bar"
    In [22]: l[44] = "bar"
    In [23]: print(l[43])
    bar
    

    P.S. I recommend that you don’t call your variable list as it shadows the list() builtin.

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

Sidebar

Related Questions

Why does this attempt at creating a list of curried functions not work? def
I have an ARM project that I'm building with make. I'm creating the list
I have been having problems with creating a download list of files for a
First off, I have read through a list of postings on this topic and
I'm creating a list of my defined objects like so List<clock> cclocks = new
Hey guys, so I am creating a List(Of String), always of size 9. This
I have a list and I am trying to find a particular element and
I'm just creating a simple list and then destroying it. And something is going
I have a checkbox list creating an array that I want to iterate over
Please help this Linq newbie! I'm creating a list inside my class under test,

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.