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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:26:21+00:00 2026-06-07T12:26:21+00:00

Can someone explain the last line of this Python code snippet to me? Cell

  • 0

Can someone explain the last line of this Python code snippet to me?

Cell is just another class. I don’t understand how the for loop is being used to store Cell objects into the Column object.

class Column(object):

    def __init__(self, region, srcPos, pos):

        self.region = region
        self.cells = [Cell(self, i) for i in xrange(region.cellsPerCol)] #Please explain this line.
  • 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-07T12:26:22+00:00Added an answer on June 7, 2026 at 12:26 pm

    The line of code you are asking about is using list comprehension to create a list and assign the data collected in this list to self.cells. It is equivalent to

    self.cells = []
    for i in xrange(region.cellsPerCol):
        self.cells.append(Cell(self, i))
    

    Explanation:

    To best explain how this works, a few simple examples might be instructive in helping you understand the code you have. If you are going to continue working with Python code, you will come across list comprehension again, and you may want to use it yourself.

    Note, in the example below, both code segments are equivalent in that they create a list of values stored in list myList.

    For instance:

    myList = []
    for i in range(10):
        myList.append(i)
    

    is equivalent to

    myList = [i for i in range(10)]
    

    List comprehensions can be more complex too, so for instance if you had some condition that determined if values should go into a list you could also express this with list comprehension.

    This example only collects even numbered values in the list:

    myList = []
    for i in range(10):
        if i%2 == 0:     # could be written as "if not i%2" more tersely
           myList.append(i)
    

    and the equivalent list comprehension:

    myList = [i for i in range(10) if i%2 == 0]
    

    Two final notes:

    • You can have “nested” list comrehensions, but they quickly become hard to comprehend 🙂
    • List comprehension will run faster than the equivalent for-loop, and therefore is often a favorite with regular Python programmers who are concerned about efficiency.

    Ok, one last example showing that you can also apply functions to the items you are iterating over in the list. This uses float() to convert a list of strings to float values:

    data = ['3', '7.4', '8.2']
    new_data = [float(n) for n in data]
    

    gives:

    new_data
    [3.0, 7.4, 8.2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain to me this odd thing: When in python shell I type
I'm sure it's intentional, so can someone explain the rationale for this behavior: Python
I was wondering if someone can explain this macro to me. #define Q_DECLARE_PRIVATE(Class) \
Can someone explain why the last assignment in the code below is not valid
Can someone explain why the last line prints out -1? It happens when copy
Can someone please explain why I'm getting this error? My code: def x(n): if
Can someone explain why the last yielder throws a no block given? class Foo
Can someone explain why this doesn't work? int main() { float* x; float a,
Can someone explain why this script throws an exception? $byteArray = @(1,2,3) write-Output (
Can someone explain how New works with the With keyword in this example from

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.