When you do for i in row, i takes the values in row, i.e. '1', then '2', then '3'.

Those are strings, which are not valid as a list index. A list index should be an integer. When doing range(len(row)) you loop on integers up to the size of the list.

By the way, a better approach is:

for elt_id, elt in enumerate(list):
    # do stuff
Answer from Mathieu on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_lists_loop.asp
Python - Loop Lists
List Comprehension offers the shortest syntax for looping through lists: A short hand for loop that will print all items in a list: thislist = ["apple", "banana", "cherry"] [print(x) for x in thislist] Try it Yourself » · Learn more about ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › iterate-over-a-list-in-python
Iterate over a list in Python - GeeksforGeeks
[print(val) for val in a] ... Note: This method is not a recommended way to iterate through lists as it creates a new list (extra space). We can use the range() method with for loop to traverse the list.
Published   December 27, 2025
Discussions

What does a for loop within a list do in Python? - Stack Overflow
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() ... More on stackoverflow.com
🌐 stackoverflow.com
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
Iterate through a list and perform an action for each item in the list
There will probably be a more elegent way to do this (and I hope someone can point this out to me) but this should work: for i, item1 in enumerate(desciption_list): for j, item2 in enumerate(desciption_list): if j <= i: continue else: print(jaro_distance(str(item1), str(item2)) Just found this on StackOverflow : import itertools for a, b in itertools.combinations(mylist, 2): print(jaro_distance(a, b)) More on reddit.com
🌐 r/learnpython
7
29
July 4, 2021
Creating lists using for loops
You wouldn't create new variables for each list. You'd add those lists into another list, then you'd just index into the outer list to get the inner lists. But yes, this is very possible. Trivial even with something like a list comprehension. More on reddit.com
🌐 r/learnpython
2
2
November 14, 2021
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.

🌐
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.
🌐
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.
🌐
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".
Find elsewhere
🌐
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 ...
🌐
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.
🌐
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?

🌐
Python Tutorial
pythontutorial.net › home › python basics › how to use a for loop to iterate over a list
How to Use a For Loop to Iterate over a List - Python Tutorial
March 26, 2025 - To iterate over a list, you use the for loop statement as follows: for item in list: # process the itemCode language: Python (python)
🌐
Lawrence
www2.lawrence.edu › fast › GREGGJ › CMSC210 › loops › for.html
Lists and for
For that purpose we introduce two variables, sum and count, and make the loop body update both variables for each member of the data list. sum = 0 count = 0 for x in data: sum += x count += 1 average = sum/count · To indent lines of code in a Python program you can use spaces or tabs.
🌐
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 ...
🌐
Quora
quora.com › How-do-you-populate-a-list-using-a-loop-Python-list-loops-development
How to populate a list using a loop (Python, list, loops, development) - Quora
Let’s see all the different ways to iterate over a list in Python, and performance comparison between them. ... Method #2: For loop and range() In case we want to use the traditional for loop which iterates from number x to number y.
🌐
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 ...
🌐
GitHub
hplgit.github.io › primer.html › doc › pub › looplist › ._looplist-bootstrap002.html
Loops and lists
When data are collected in a list, we often want to perform the same operations on each element in the list. We then need to walk through all list elements. Computer languages have a special construct for doing this conveniently, and this construct is in Python and many other languages called a for loop. Let us use a for loop to print out all list elements:
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python iterate over list
Python Iterate Over List - Spark By {Examples}
May 31, 2024 - Use for item in list syntax to iterate over a list in Python. By using this for loop syntax you can iterate any sequence objects
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop - Python Wiki
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. Contrast the for statement with the ''while'' loop, used when a condition needs to be checked ...
🌐
Projectpython
projectpython.net › chapter07
Lists and for-loops - Project Python
Because for-loops are simpler and easier to read, you should use a for-loop in your code wherever the goal is to refer to the items of a list, but without changing the list. If you want to change which items the list contains, you should use a while-loop. Some functions return lists.