How to initialize a two-dimensional array in Python?

873    Asked by AmyAvery in Python , Asked on Apr 18, 2021

I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:

def initialize_twodlist(foo):

twod_list = [] 

new = [] 

for i in range (0, 10):

for j in range (0, 10):

new.append(foo)

twod_list.append(new)

new = []

It gives the desired result but feels like a workaround. Is there an easier/shorter/more elegant way to do this?

Answered by Anurag Singhal

Your Answer

Interviews

Parent Categories