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

  • Home
  • SEARCH
  • 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 8702465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:39:38+00:00 2026-06-13T02:39:38+00:00

In Python… I have a nested list defined in a class as: self.env =

  • 0

In Python…

I have a nested list defined in a class as:

self.env = [[ None for i in range(columnCount)] for j in range(rowCount) ]       

For the purpose of this question, lets assume this nested list contained values of:

[None, None, None, None]
[None, None, None, None]
[None, None, 0, None]
[None, None, None, None]

I need to be able to leverage the iter and next() methods to go through this list in column major order (row0,col0 row1,col0, row2,col0…).

The intent is for it to send the value located in that grid location to another function to get processed.

So far I have the iter function defined as:

def __iter__(self):
    return iter(self.env)

Then I have the next function defined as:

def next(self):
    self.currentRow += 1
    self.currentColumn += 1

    if ( (self.currentRow < self.getRowCount()) and (self.currentColumn < self.getColumnCount()) ):
        return self.env[self.currentRow][self.currentColumn]
    else:
        raise StopIteration

The problem I am having is that it seems the iter function is return each row as a list, but it is not making a call to the next method to further process that list.

My objective is to get print the values at each grid location invdividually… as in:

None
None
…
None
0
None
…
None

Can someone elaborate on what it is I am missing in order to accomplish 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-06-13T02:39:39+00:00Added an answer on June 13, 2026 at 2:39 am

    Your __iter__ function is returning iter(self.env), which returns the iterator for self.env. If you want your next method to be called, __iter__, you should return self. But there are other issues in your implementation, for example, your iterator won’t work more than once.

    There are easier ways to achieve this. You can write a Generator Expression/List Comprehensions to achieve this (see @Eric’s answer). You can also write a generator method. E.g.:

    def iter_column_row():
        for i in range(self.getColumnCount()):
            for j in range(self.getRowCount()):
                yield self.env[j][i]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python beginner here, I have a list of lists and want to refer to
Python 3 really complicated the whole file reading process, when you have a binary
(Python) Given two numbers A and B. I need to find all nested groups
Python beginner here. I have a text file that is sorted into columns: fields
[Python/MySQLdb] - CentOS - Linux - VPS I have a page that parses a
Python allows to call a static method not only from a class, but also
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
Python decorators are fun to use, but I appear to have hit a wall
Python 2.5 won't let me use this syntax: try: code_that_raises_exception() except Exception as e:
Python is installed in a directory all users have full access to, but when

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.