Generator comprehension - list None

Chris Angelico rosuav at gmail.com
Tue Oct 18 07:55:46 EDT 2016


On Tue, Oct 18, 2016 at 10:43 PM, Sayth Renshaw <flebber.crue at gmail.com> wrote:
> I was solving a problem to create a generator comprehension with 'Got ' and a number for each in range 10.
>
> This I did however I also get a list of None. I don't understand where none comes from. Can you please clarify?
>
> a = (print("Got {0}".format(num[0])) for num in enumerate(range(10)))
>

The print function returns None. When you create a comprehension or
generator expression, you're collecting up the values of the
expressions - in this case, the return values from the print calls -
as the end result. If you don't want that, use a regular 'for' loop.

ChrisA



More information about the Python-list mailing list