+ 1

For loop in python

What is the different thing between 'i for i' and 'for i' ?

9th Jun 2019, 7:49 PM
Saranyapond Samanrad
4 Answers
+ 5
Aah, okay, in comprehensions, I see!
9th Jun 2019, 9:25 PM
HonFu
HonFu - avatar
+ 4
The second one... exists. ;-) Where did you find 'i for i'?
9th Jun 2019, 8:41 PM
HonFu
HonFu - avatar
+ 2
"i for i" can be used to easily make lists or other iterables, where each item follows a certain logic. With timeit.timeit I discovered that "i for i" can take a large list without it even affects it's generation speed much, which means that "i for i" can be faster than "for i". "i for i" only supports one line statements, but that can be fixed by functions. "for i" loops supports multiple lines and can increase readability.
9th Jun 2019, 9:19 PM
Seb TheS
Seb TheS - avatar
9th Jun 2019, 8:56 PM
Seb TheS
Seb TheS - avatar