Why no colon in forming a list from loop in one line in Python?

2024/10/5 19:13:55

From this website, there is a way to form a list in Python from loop in one line

squares = [i**2 for i in range(10)]

My question is, typically, after a loop, there is a colon, e.g.,

squares = []
for i in range(10):squares.append(i**2)

why in the one-line approach, there is no colon? Is there any underlying reason than convention?

Answer

Note (in addition to what have been said in comments: it is syntax of the language. There is usually no obvious reason to "why" a syntax rule exist, other that "because". Because the author decided so), that this is not the same for. Those are two different meanings of the keyword for. The reason why the same keyword could be used for both usage is because context makes it never ambiguous. And one of the element of context that makes it unambiguous is precisely the presence of absence of colon.

The first for is a control structure, deciding in which order are executed imperative instruction

for i in iterable:doSomething

instruct the interpreter to repeat the imperative instruction doSmething manytimes, one, for each values in the iterable, and with a local environment containing i being those values one after each other. No need to say more. You certainly already know what a for loop is. I describe it only for contrast to the next usage

[expr(i) for i in iterable]

This is NOT a for loop. It has nothing, or very few, to do with previous instruction. To start with, it is not even an instruction! This is an expression. [expr(i) for i in iterable] does nothing (unless you implemented some "side-effect" to expr(i) or even to the iteration of the iterable, so let say, it, a priori does nothing). It does nothing but has a value. So it is the same difference as between the instruction print(12) which has no value, but does something, and the expression 12 which does nothing, but has a value.

So there is no reason to find the same syntax, and in particular the colon, separating the for part from the list of instructions that are supposed to be repeated, since there is no list of instructions.

You can find the same difference (and answer would probably have been more easy to spot with this second example) with the if keyword.

if condition:doSomething()
else:doSomethingElse()

is an instruction.

12 if condition else 15

is an expression.

The first one has no value. You can't

x=if condition:doSomething
else:doSomethingElse

But has effect.

The second one has a priori no effect, but has a value. You can

x=12 if condition else 15

So the first one is the same as print(something) when the second one is the same as 12. The first does something, the second's value is something.

So, here again, it is not at all the same if. The context makes it obvious which one it is (for example because the first one use a colon, the second doesn't). But those are two very different part of the language (even tho, of course, like for for, we easily see why, from a human point of view, it makes sense to call both if, or all both for for your initial example)

There are some languages, for example C, where those two construct have not at all the same name. Proving that, indeed, these are not the same thing. In C, the first one would be

if(condition){doSomething();
}else{doSomethingElse();
}

While the second one would be

condition?12:15

for example

x=condition?12:15

My point, by bringing C, is to show that those two if have nothing to do. In C, one is if the other is the ternary operator ...?...:....

In python, the ternary operator and the if control structure happen to use the same keyword, but with different syntax, that's all.

The reason why I brought up if example, besides being a second example worth noting of language syntax that has nothing to do which each other, while sharing a same keyword, is because with for it would have been harder for me to show you examples of other languages where the 2 things use different keywords, for the simple reason that python's [... for ...] is quite unique, at least among most classical languages. There is no equivalent of that in C, C++, C#, Java, Javascript, etc. But yet, this is not a for loop.

So, short answer: [... for ...] doesn't have a colon like in for loops, because it is not a for loop.

https://en.xdnf.cn/q/119546.html

Related Q&A

Merge each groups rows into one row

Im experienced with Pandas but stumbled upon a problem that I cant seem to figure out. I have a large dataset ((40,000, 16)) and I am trying to group it by a specific column ("group_name" for…

Python decode unknown character

Im trying to decode the following: UKLTD� For into utf-8 (or anything really) but I cannot workout how to do it and keep getting errors likeascii codec cant decode byte 0xae in position 8: ordinal not…

UnboundLocalError: TRY EXCEPT STATEMENTS

I am currently creating a menu with try except tools. Im trying to create it so if a user enters nothing (presses ENTER) to output:You have not entered anything, please enter a number between 1 and 4Th…

Cant load music into pygame

please help if you can. Cant seem to be able to upload music into my game in progress. It comes up with the error of "cant load"... Would be great if someone got back to me quick, This is a m…

C# Socket: how to keep it open?

I am creating a simple server (C#) and client (python) that communicate using sockets. The server create a var listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp)then …

Python Selenium - how to get confirmation after submit

I have a follow up question on this post, I want to get any confirmation text after I hit submit button. Either the code works or not. html - invalid example <div class="serialModalArea js-seri…

Assigning a input on a line to its line number?

I was having a go at the following problem from the AIO (Australian Informatics Olympiad) Training Problems Site (Question in Italics and specifics in bold, my attempt below): The Problem Encyclopaedia…

Create a new list according to item value

I have a list like below. [T46, T43, R45, R44, B46, B43, L45, L44, C46, C45]where I want to group according to int value:[id][ , , , , ] # AREA PATTERN [Top, Right, Bottom, Left, Center][46][1,0,1…

Concatenating many time and date columns

I have many date and time column pairs (around 15 each) that share the same prefix, ie. SH or DEL. The columns are all of dtype object, ie. string. All the columns belong to the same Dataframe. Here is…

Python with ICS files and CSV [duplicate]

This question already has answers here:Parsing files (ics/ icalendar) using Python(6 answers)Closed 5 years ago.one friend ask me some help on a personnal project, but I have to admit my skills in Pyth…