[Tutor] Nested list comprehensions

Manprit Singh manpritsinghece at gmail.com
Sun Oct 18 01:39:20 EDT 2020


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


More information about the Tutor mailing list