1
1
1432020-05-27 22:17:18
Python
143, 2020-05-27 22:17:18

Python list generator?

Hai! There is a list generator:
i = [x for x in range(1, 1000)]
I can't figure out why x is in front of for?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
galaxy, 2020-05-27
@galaxy

The expression specifies for the generator. Here it is the simplest and looks like a tautology, but not always the same:
[x**2 for x in range(1, 1000)]

M
milssky, 2020-05-27
@milssky

the same thing without a generator, you can somehow write like this.

i = []
for x in range(1,1000):
    i.append(x)

N
Nikolay, 2020-05-27
@SODINNER

Before for, what is added to the list is not necessarily even a temporary variable from the loop, it can be anything, even though the constant number 1 and after each iteration the number 1 will be added to the list.
And after you have entered what will be added to the list, you enter the cycle itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question