[Tutor] list generation

David Lowry-Duda david at lowryduda.com
Mon May 10 17:01:27 EDT 2021


On Mon, May 10, 2021 at 01:59:08PM -0500, 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' ]

It's not obvious to me what "best" means here, but something like

    def gen_list(val, num):
        return [val]*num

seems to get pretty far.

This sounds to me like a secondary problem, sought to solve some main 
problem. It might be a good idea to go ahead and specify the main 
problem too, if there is one.

- DLD


More information about the Tutor mailing list