Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Python
  3. Question
Python

How to initialize a two-dimensional array in Python?

Asked by Amy Avery Apr 18, 2021 1.6K views 1 answer
Share

About this question

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?

Your answer

1 Answer

More Python discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Python Blogs

Guides, tips and career advice on Python from JanBask experts.