Add to empty list using single line for-loop python

I am trying to add numbers from a generator to an empty list using one line for a loop, but returns None . I understand that this can be done using a for loop with two lines, but I was wondering what I did not see. those.

>>> [].append(i) for i in range(10)

[None, None, None, None, None, None, None, None, None, None]

I was hoping to create this on one line:

>>> [].append(i) for i in range(10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Thanks.

+5
source share
1 answer

Write the correct understanding without adding.

 >>> [i for i in range(10)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(i for i in range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
+6
source

Source: https://habr.com/ru/post/1212163/


All Articles