It's called list comprehension and, in this case, simply creates a list of elements, all with the integer value 4, for each of the items in the for
section (the values 0 through 4 inclusive).
But int(4)
is not really required, you can use 4
instead. However, even that's overkill for this particular situation, I'd prefer the more succinct:
[4] * 5
myself, leaving the for
-loop form for more complicated stuff such as getting the cubes of odd numbers whose squares are less that a given value:
>>> [i**3 for i in range(99) if i*i < 99 and i%2 == 1]
[1, 27, 125, 343, 729]
Yes, a rather contrived example, but it shows how you can use list comprehensions to do some truly powerful stuff.