The "brackets" in your example constructs a new list from an old one, this is called list comprehension.

The basic idea with [f(x) for x in xs if condition] is:

def list_comprehension(xs):
    result = []
    for x in xs:
        if condition:
            result.append(f(x))
    return result

The f(x) can be any expression, containing x or not.

Answer from folkol on Stack Overflow
Discussions

list - Using python for loop inside brackets - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Brackets after range() method in for loop?
What is this doing, exactly? Making up for the fact that the author doesn't know how to use range() /s More on reddit.com
🌐 r/learnpython
17
10
August 31, 2021
For Loops with Curly Brackets -- Please explain
Here is where you'll find the documentation for this method of formatting. More on reddit.com
🌐 r/learnpython
4
9
December 27, 2017
Python for-loop in brackets vs parenthesis - Stack Overflow
I just started some months ago learning python3. I was curious where the difference between the following code is and when to use it. #A: for i in [x*2 for x in range(100)]: if i == 2: ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
HowDev
how.dev › answers › how-can-we-loop-through-a-list-using-a-for-loop-in-python
How can we loop through a list using a for loop in Python?
To iterate through a list in Python, use a for loop to print each element individually until all elements are processed.
🌐
Stack Overflow
stackoverflow.com › questions › 60727590 › using-python-for-loop-inside-brackets
list - Using python for loop inside brackets - Stack Overflow
Actually I encounter one code today which is shown below: def solution(ar,n): d={i: ar[i] for i in range(n)} for i in range(n-1): for j in range(i+1,n-1): if(ar[i]+ar[j...
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Iteration › TheforLoop.html
7.2. The for Loop — Foundations of Python Programming
name in this for statement is called the loop variable or, alternatively, the iterator variable. The list of names in the square brackets is the sequence over which we will iterate.
Find elsewhere
🌐
Dataquest
dataquest.io › blog › tutorial-advanced-for-loops-python-pandas
Tutorial: Advanced Python for Loops – Dataquest
March 11, 2025 - We'll skip lists since those have been covered in the previous tutorial; if you need further review, check out the introductory Python for loops tutorial or Dataquest's interactive lesson on lists and for loops. Tuples are sequences, just like lists. The difference between tuples and lists is that tuples are immutable; that is, they cannot be changed (learn more about mutable and immutable objects in Python). Tuples also use parentheses instead of square brackets.
🌐
Reddit
reddit.com › r/learnpython › for loops with curly brackets -- please explain
r/learnpython on Reddit: For Loops with Curly Brackets -- Please explain
December 27, 2017 -

I am trying to understand this code. I understand that first for loop is iterating over the comments using the message column. However, why is a second for loop required and what do the curly braces do?

   for comment in comments[' Message']:
               s = sentiment.polarity_scores(comment)
               for k in sorted(s):
               print('{0}: {1}, '.format(k,s[k]))
   print(comment)
🌐
University at Buffalo
math.buffalo.edu › ~badzioch › MTH337 › PT › PT-lists.html
Lists, ranges and “for” loops — MTH 337
Python for loops provide a way to iterate (loop) over the items in a list, string, or any other iterable object, executing a block of code on each pass through the loop.
🌐
W3Schools
w3schools.com › python › gloss_python_array_loop.asp
Python Loop Through an Array
Python Examples Python Compiler ... Python Bootcamp Python Certificate Python Training ... You can use the for in loop to loop through all the elements of an array....
🌐
Projectpython
projectpython.net › chapter07
Lists and for-loops - Project Python
You can type a list in using brackets and commas. When the Python interpreter reaches the brackets, Python allocates space in memory for the items in the list. Of course, usually there’s not much point in just printing out a list created this way; we’d like to be able to refer to the list ...
🌐
Stack Overflow
stackoverflow.com › questions › 70567743 › python-for-loop-in-brackets-vs-parenthesis
Python for-loop in brackets vs parenthesis - Stack Overflow
So the first one(A) is in square brackets and the other one(B) is in "normal" brackets. Where is the difference between them, and which one is perferably better in this kind of case.
🌐
Tutorialspoint
tutorialspoint.com › python › python_loop_arrays.htm
Python - Loop Arrays
import array as arr # creating array a = arr.array('i', [96, 26, 56, 76, 46]) # checking the length l = len(a) # loop variable idx = 0 # while loop while idx < l: print (a[idx]) # incrementing the while loop idx+=1 · On executing the above code, it will display the following output − ... We can find the length of array with built-in len() function. Use it to create a range object to get the series of indices and then access the array elements in a for loop.
🌐
NumPy
numpy.org › doc › stable › reference › arrays.nditer.html
Iterating over arrays — NumPy v2.4 Manual
For the nditer object, this means letting the iterator take care of broadcasting, dtype conversion, and buffering, while giving the inner loop to Cython. For our example, we’ll create a sum of squares function. To start, let’s implement this function in straightforward Python.
🌐
Codingem
codingem.com › home › python for loops—a complete guide & useful examples
Python For Loops—A Complete Guide & Useful Examples
December 4, 2022 - A list of useful for loop examples in python. Learn how to loop your code like a pro with beginner-friendly examples and explanations.
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node58.html
for loops and the range function
If you're familiar with other ... like this are solved by iterating over a sequence of integers created by the range function, and then refering to the individual elements of the sequence inside the body of the for loop to perform the desired tasks....
🌐
Python Morsels
pythonmorsels.com › writing-a-for-loop
Python's "for" loop - Python Morsels
September 29, 2020 - Lists in Python are iterables. Here we have a variable favorite_fruits that points to list of strings: >>> favorite_fruits = ["jujube", "pear", "watermelon", "apple", "blueberry"] We can loop over (aka "iterate over") this favorite_fruits list using a for loop: