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]
Answer from Levon on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_lists_loop.asp
Python - Loop Lists
Learn more about while loops in our Python While Loops Chapter. List Comprehension offers the shortest syntax for looping through lists:
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
We can also use the enumerate() function to iterate through the list. This method provides both the index (i) and the value (val) of each element during the loop. ... a = [1, 3, 5, 7, 9] # Here, i and val reprsents index and value respectively for i, val in enumerate(a): print (i, val)
Published   December 27, 2025
Discussions

For loop with list
First I need to make a list of names: sailors = [“Jan”, “Piet”, “Joris”, “Korneel”] Then I need to ask the user to input his/her name: name = input (“Type your name please”.) Then, and HERE is my problem, I need to use a for loop to check if the name is in the list. More on discuss.python.org
🌐 discuss.python.org
0
0
February 28, 2023
What does a for loop within a list do in Python? - Stack Overflow
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. c... More on stackoverflow.com
🌐 stackoverflow.com
python - Creating for loop until list.length - Stack Overflow
I'm reading about for loops right now, and I am curious if it's possible to do a for loop in Python like in Java. ... In Java, that loop is going to skip the first element of your list. Is that really what you want? If so, just use my_list[1:] instead of my_list in all of the answer below. More on stackoverflow.com
🌐 stackoverflow.com
Iterating over a list in python using for-loop - Stack Overflow
I have a question about iterating over a list in python. Let's say I have a list: row = ['1', '2', '3'] and want to convert its element to integers, so that: row = [1, 2, 3]. I know I can do it w... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Tutorialspoint
tutorialspoint.com › python › python_loop_lists.htm
Python - Loop Lists
In the following example, we are using a for loop to iterate through each element in the list "lst" and retrieving each element followed by a space on the same line − · lst = [25, 12, 10, -21, 10, 100] for num in lst: print (num, end = ' ') ... A while loop in Python is used to repeatedly execute a block of code as long as a specified condition evaluates to "True".
🌐
Python.org
discuss.python.org › python help
For loop with list - Python Help - Discussions on Python.org
February 28, 2023 - First I need to make a list of names: sailors = [“Jan”, “Piet”, “Joris”, “Korneel”] Then I need to ask the user to input his/her name: name = input (“Type your name please”.) Then, and HERE is my problem, I need to use a for loop to check if the name is in the list.
Top answer
1 of 2
89

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]
2 of 2
1

It is the same as if you did this:

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

This is called a list comprehension.

🌐
Codecademy
codecademy.com › learn › dspath-python-lists-and-loops › modules › dspath-loops › cheatsheet
Python Lists and Loops: Python Loops Cheatsheet | Codecademy
In Python, a for loop can be used to perform an action a specific number of times in a row. The range() function can be used to create a list that can be used to specify the number of iterations in a for loop.
Find elsewhere
🌐
LearnPython.com
learnpython.com › blog › python-list-loop
7 Ways to Loop Through a List in Python | LearnPython.com
As you can see, we created the fruits list just as we did in the previous example. However, this time we used list comprehension to do two things: add the word ‘juice’ to the end of the list item and print it. Another method for looping through a Python list is the range() function along with a for loop.
🌐
StrataScratch
stratascratch.com › blog › looping-through-lists-in-python
Looping Through Lists in Python: A Comprehensive Tutorial - StrataScratch
April 9, 2025 - That’s a recipe for bugs. Always work with a copy (or create a new list to work from). Second, when tracking positions or working with multiple lists, use tools such as `enumerate()` or `zip()`. They’re also more than fancy—they prevent index headaches. And if your loop can be expressed in a single line, don’t overthink it. Python rewards silence and list comprehensions there for a reason.
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop - Python Wiki
for loops are used when you have a block of code which you want to repeat a fixed number of times. The for-loop is always used in combination with an iterable object, like a list or a range. The Python for statement iterates over the members of a sequence in order, executing the block each time.
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Iteration › Listsandforloops.html
7.5. Lists and for loops — Foundations of Python Programming
Yes, there are nine elements in the list so the for loop will iterate nine times. ... Iteration by item will process once for each item in the sequence. Each string is viewed as a single item, even if you are able to iterate over a string itself. Error, the for statement needs to use the range function. The for statement can iterate over a sequence item by item. How does python ...
🌐
EITCA
eitca.org › home › how can we iterate over a list of lists using a "for" loop in python?
How can we iterate over a list of lists using a "for" loop in Python? - EITCA Academy
August 3, 2023 - To iterate over a list of lists using a "for" loop in Python, we can make use of nested loops. By nesting a "for" loop inside another "for" loop, we can traverse through each element in the outer list and then iterate over the inner lists. Here's an example to illustrate the process: python ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › create-a-list-of-lists-using-for-loop
Create A List of Lists Using For Loop - Python
July 23, 2025 - In Python, creating a list of lists using a for loop involves iterating over a range or an existing iterable and appending lists to the main list. This approach allows for the dynamic generation of nested lists based on a specific pattern or ...
🌐
Reddit
reddit.com › r/learnpython › iterate through a list and perform an action for each item in the list
r/learnpython on Reddit: Iterate through a list and perform an action for each item in the list
July 4, 2021 -

Hi, so I am quite new to python however have an issue I am struggling to come up with convincing solution to.

I have a list of strings - around 27 items - and have also created a function that calculates the Jaro distance between the two String variables, this works as far as I can tell however I now need to form a loop that will allow me to apply this function for each item in the list against each other item in the list.

So far I have been using the following for loop:

for i in description_list:
    print(jaro_distance(str(description_list[1]),str(description_list[count+1])))     
    count = count + 1

print(jaro_distance(str(description_list[1]), str(description_list[count+1]))) count = count + 1

but this only works for comparing the fist item in the list vs the following items. So my question is this, how can I create a loop that iterates each item against every other item in the list?

🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python iterate over list
Python Iterate Over List - Spark By {Examples}
May 31, 2024 - In this example, enumerate(courses, start=1) starts the index from 1 instead of the default 0. The loop then prints the index and the corresponding course for each element in the list. # Start loop indexing with non zero value courses=['java','python','pandas','sparks'] for index , item in enumerate(courses,start=1): print(index,item) # Output: # 1 java # 2 python # 3 pandas # 4 sparks
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
Remove List Duplicates Reverse ... Q&A Python Bootcamp Python Certificate Python Training ... A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string)....
🌐
Lawrence
www2.lawrence.edu › fast › GREGGJ › CMSC210 › loops › for.html
Lists and for
This is our first example of a Python for loop. A for loop is an iteration structure that is designed to iterate over each member of some list of data values. For each member of the list the for loop temporarily assigns the member to the variable x. In the body of the loop (the line of code ...