[Tutor] Nested list comprehensions

Mark Lawrence breamoreboy at gmail.com
Sun Oct 18 03:29:55 EDT 2020


On 18/10/2020 06:39, Manprit Singh wrote:
> Dear sir ,
> 
> While reading about nested list comprehensions, i have tried to write an
> example of making a 2D list with m rows and n columns with each element of
> list equals e.
> Just need to know , if my understanding is correct or not ?
> 
> def two_d(row, col, ele):
>      return [[ele for _ in range(col)] for _ in range(row)]
> 
> m = int(input("Enter no of rows"))
> n = int(input("Enter no of columns"))
> e = int(input("Enter the element to be placed inside list'))
> two_d(m, n, e)
> 
> Regards
> Manprit Singh
> 

I don't intend doing your testing for you, so please try the code in the 
interactive interpreter or place the code in a script and run it.

-- 
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