In Python you can iterate over the list itself:

for item in my_list:
   #do something with item

or to use indices you can use xrange():

for i in xrange(1,len(my_list)):    #as indexes start at zero so you 
                                    #may have to use xrange(len(my_list))
    #do something here my_list[i]

There's another built-in function called enumerate(), which returns both item and index:

for index,item in enumerate(my_list):
    # do something here

examples:

In [117]: my_lis=list('foobar')

In [118]: my_lis
Out[118]: ['f', 'o', 'o', 'b', 'a', 'r']

In [119]: for item in my_lis:
    print item
   .....:     
f
o
o
b
a
r

In [120]: for i in xrange(len(my_lis)):
    print my_lis[i]
   .....:     
f
o
o
b
a
r

In [122]: for index,item in enumerate(my_lis):
    print index,'-->',item
   .....:     
0 --> f
1 --> o
2 --> o
3 --> b
4 --> a
5 --> r
Answer from Ashwini Chaudhary 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
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
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 11, 2021
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
🌐
Claude
code.claude.com › docs › en › how-claude-code-works
How Claude Code works - Claude Code Docs
1 day ago - Understand the agentic loop, built-in tools, and how Claude Code interacts with your project.
🌐
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".
🌐
Real Python
realpython.com › python-for-loop
Python for Loops: The Pythonic Way – Real Python
3 weeks ago - Python’s for loop allows you to iterate over the items in a collection, such as lists, tuples, strings, and dictionaries. The for loop syntax declares a loop variable that takes each item from the collection in each iteration.
Find elsewhere
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop
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.
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.

🌐
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.
🌐
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.
🌐
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 documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended): >>> # Measure some strings: >>> words = ['cat', 'window', 'defenestrate'] >>> for w in words: ... print(w, len(w)) ... cat 3 window 6 defenestrate 12 · Code that modifies a collection while iterating over that same collection can be tricky to get right. Instead, it is usually more straight-forward to loop over a copy of the collection or to create a new collection:
🌐
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)....
🌐
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 11, 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?

🌐
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
Answer (1 of 3): Often, you combine the two things together into a comprehension: [code]squares = [x*x for x in numbers] even_squares = [x*x for x in numbers if x%2==0] [/code]The first one populates a list by looping over [code ]numbers[/code], and, for each one, putting [code ]x*x[/code] into ...
🌐
Switowski
switowski.com › blog › for-loop-vs-list-comprehension
For Loop vs. List Comprehension
List comprehension with a separate transform() function is around 17% slower than the initial "for loop"-based version (224/191≈1.173). But it's much more readable, so I prefer it over the other solutions. And, if you are curious, the one-line list comprehension mentioned before is the fastest solution: def fizz_buzz_comprehension(): return [ "fizzbuzz" if x % 3 == 0 and x % 5 == 0 else "fizz" if x % 3 == 0 else "buzz" if x % 5 == 0 else x for x in MILLION_NUMBERS ] $ python -m timeit -s "from filter_list import fizz_buzz_comprehension" "fizz_buzz_comprehension()" 2 loops, best of 5: 147 msec per loop
🌐
GUVI
guvi.in › hub › python › iterate-over-a-list-in-python
Iterate Over a List in Python
Using a 'for' loop: The 'for' loop is a simple and effective way to iterate over a list in Python. It iterates over each element in the list and executes a block of code for each iteration.