[Tutor] Nested list comprehensions

dn PyTutor at DancesWithMice.info
Sun Oct 18 03:39:36 EDT 2020


On 18/10/2020 18: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)

What appears when you print the returned result?
(the result of the function which is 'lost', at present)

NB to make it easier to read, import and use pprint (pretty-print) 
instead of print() and the default format for list(s).
-- 
Regards =dn


More information about the Tutor mailing list