1

I know that [] indicates a list in Python. The original example was

[classify_single_case(icmaTrainingData_df, icmaTargetValues_ss, i, 5) for i in icmaTrainingData_df.index]

But for the purpose of this question, we can simplify it as:

[print(i) for i in range(0,5)]

I'm used to normal "for loops", e.g.

for i in range(0,5): print(i)

I couldn't find it in the official documentation, e.g. https://docs.python.org/3/tutorial/controlflow.html#for-statements therefore without fully understanding it I'm not comfortable with using it.

Can all single statement for loops be enclosed with [] signs? Is this some sort of "code golf" attempt at writing shorter code?

user96769
  • 25
  • 5
  • 1
    This is a list comprehension, which isn't the same as a for-loop. It always *creates a list*. – juanpa.arrivillaga Apr 06 '20 at 10:23
  • 1
    These are called list comprehensions. They're a more compact, faster way of generating lists that are typically generated in a one line for loop. The documentation is here. https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions – Axe319 Apr 06 '20 at 10:23
  • 1
    @Axe319 list comprehension aren't really faster, for very simple operations, they are somewhat faster, but usually it is negligible (mostly due to caching the resolution of `list.append` in stead of re-evaluating it each iteration of the loop, but other than that, it is implemented as a regular for-loop, you can "cache" that too doing `foo_append = foo.append` and using `foo_append` in the loop. There main use-case is readability. – juanpa.arrivillaga Apr 06 '20 at 10:25
  • 1
    list comp is noted in this area of the docs: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions – Phillyclause89 Apr 06 '20 at 10:27
  • 1
    Note that enclosing the expression in `[]` is what makes it a `list`. The portion of code in between is actually a **generator** and can be used in other containers, or even saved "as is" for future use. – norok2 Apr 06 '20 at 10:28
  • @norok2 no, the portion inside is not a generator. No generator is created. If you omit the square brackets, it becomes a generator expression, but those are two distinct syntaxes. A list comprehension isn't a generator expression inside a list list litera anyway. And even underneath the hood, `[x for x in whatever]` isn't implemented as `list(x for x in whatever)`. – juanpa.arrivillaga Apr 06 '20 at 12:03
  • @juanpa.arrivillaga Not sure why you say so. I understand that `[x for x in ...]` may have some optimization that bypasses the creation of a generator, but I am not sure what is the problem in thinking of `[x for x in ...]` as `list(x for x in ...)`, even if underneath the former is more optimized. – norok2 Apr 06 '20 at 12:18
  • because it simply isn't the case. In no sense is it *actually* a generator. It isn't an optimization. It's a part of the grammar, they are two different expressions. You can use `ast.parse` to see what Python sees. – juanpa.arrivillaga Apr 06 '20 at 12:26
  • @juanpa.arrivillaga In [PEP289](https://www.python.org/dev/peps/pep-0289/) it is stated that "...the semantic definition of a list comprehension in Python 3.0 will be equivalent to list()". Isn't this an indication that whatever is done for the case of a list comprehension syntax by the CPython parser is an implementation detail? – norok2 Apr 07 '20 at 08:26

0 Answers0