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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:18:33+00:00 2026-06-12T18:18:33+00:00

I have a nested list, named env, created in the constructor and another method

  • 0

I have a nested list, named env, created in the constructor and another method to populate an element of the grid defined as below:

class Environment(object):
    def __init__(self,rowCount,columnCount):
        env = [[ None for i in range(columnCount)] for j in range(rowCount) ] 
        return env       

    def addElement(self, row, column):
        self[row][column] = 0

Later in the code I create an instance of Environment by running:

myEnv = createEnvironment(6,6)

Then I want to add an element to the environment by running:

myEnv.addElement(2,2)

So what I expected to happen was that I would receive a new Environment object as a 6×6 grid with a 0 in position 2,2 of the grid. But that did not work.

I have two errors:

  1. I am unable to return anything other than None from the init method.
  2. The main issue us when trying to execute addElement(2, 2) I get this error:

    "TypeError: 'Environment' object does not support indexing.

I looked at the __getitem__ and __setitem__ methods but was unable to get them working over a multidimensional list. Is there a better data structure I should be using to create a grid?

  • 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-12T18:18:35+00:00Added an answer on June 12, 2026 at 6:18 pm

    The problem here is that you can’t replace the object with __init__. You could subclass list and do something in __new__, probably, but that would be massive overkill, the better option is just to wrap the list:

    class Environment(object):
        def __init__(self, rows, columns):
            self.env = [[None for column in range(columns)] for row in range(rows) ] 
    
        def addElement(self, row, column):
            self.env[row][column] = 0
    

    Note that it’s a little odd you claim to be calling myEnv = createEnvironment(6,6) – using a function rather than the constructor is a little odd.

    If you really want your object to act like the list, you can of course provide a load of extra wrapper functions like __getitem__/__setitem__. E.g:

    def __getitem__(self, row, column):
        return self.env[row][column]
    

    Which would allow you to do some_environment[5, 6], for example. (You may rather return the column, that depends on your system and what works best for you).

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

Sidebar

Related Questions

I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
I have a nested list in say lst (all the elements are of class
In Sencha Touch 2.1, I have the following nested list defined: xtype: 'NestedList', docked:
I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g., nested_list
I have a nested list something like this: PLACES = ( ('CA', 'Canada', (
I have Python nested list that I'm trying to organize and eventually count number
I have a nested unsorted list and I want to place a click event
I have a nested oredered list which i m animating using this code... var
I have a nested ul/li list <ul> <li>first</li> <li>second <ul> <li>Third</li> </ul> </li> ...

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.