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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:55:57+00:00 2026-05-30T16:55:57+00:00

Im trying to create an indexed 2D array within Python, but I keep running

  • 0

Im trying to create an indexed 2D array within Python, but I keep running into errors, one way or another.

The following code:

#Declare Constants (no real constants in Python)
PLAYER = 0
ENEMY = 1
X = 0
Y = 1
AMMO = 2
CURRENT_STATE = 3
LAST_STATE = 4

#Initilise as list
information_state = [[]]
#Create 2D list structure
information_state.append([PLAYER,ENEMY])
information_state[PLAYER].append ([0,0,0,0,0])#X,Y,AMMO,CURRENT_STATE,LAST_STATE
information_state[ENEMY].append([0,0,0,0,0])#X,Y,AMMO,CURRENT_STATE,LAST_STATE


for index, item in enumerate(information_state):
        print index, item

information_state[PLAYER][AMMO] = 5

Creates this output:

0 [[0, 0, 0, 0, 0]]
1 [0, 1, [0, 0, 0, 0, 0]]
IndexError: list assignment index out of range

Im used to using PHPs arrays, eg:

$array['player']['ammo'] = 5;

Is there anything similar in Python? I heard people recommending numpy, but i couldn’t figure it out 🙁

Im new to this Python stuff.

Note: Using Python 2.7

  • 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-30T16:55:59+00:00Added an answer on May 30, 2026 at 4:55 pm

    i think you should have a look at python’s data structures tutorial and what you’re looking for is called a dictionary here, which is a list of key-value pairs.

    in your case, you could use a nested dictionary as a value for a key, so that you could call

    ## just examples for you ##
    
    player_dict_info = {'x':0, 'y':0, 'ammo':0}
    enemy_dict_info = {'x':0, 'y':0, 'ammo':0}
    information_state = {'player': player_dict_info, 'enemy': enemy_dict_info}
    

    and access every element like you did in php

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

Sidebar

Related Questions

I'm trying to create an indexed view and get the following error creating the
I am trying to create custom array indexed from 1 using subscript operator. Getting
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create a user account in a test. But getting a Object reference
Trying to create a list to return some JSON data to a view. Following
I am trying to find the best way to create a dynamic meta robots
I am trying to create a pacman-like game. I have an array that looks
I'm trying to create and zero an array of int s based on a
I am trying to create a method that removes duplicates from a 2d array.
I was trying to create an 2d list in python that was x +

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.