[Tutor] list generation

Mark Lawrence breamoreboy at gmail.com
Mon May 10 16:53:16 EDT 2021


On 10/05/2021 19:59, Ed Connell wrote:
> What is the best way to create a list of constant values of a given length?
> 
> gen_list( 78, 4 ) should generate [ 78, 78, 78, 78  ].
> 
> and
> 
> gen_list( 'pop', 2 ) should produce [ 'pop', 'pop' ]
> 
> Thanks,
> 
> Ed
> 

https://docs.python.org/3/library/itertools.html#itertools.repeat

My personal opinion is that if you don't reach for the itertools module 
fairly often you're not really writing python :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list