Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Make a 2d array with object in python

This is how to prepopulate a two dimensional list of lists (aka array of arrays) with objects in python.

Code

from pprint import pprint

  grid = []

  rows = 4
  cols = 3

  for i in range(0, rows):
    grid.append([{"k": "v"}] * cols)

  pprint(grid)

Results

[[{'k': 'v'}, {'k': 'v'}, {'k': 'v'}],
   [{'k': 'v'}, {'k': 'v'}, {'k': 'v'}],
   [{'k': 'v'}, {'k': 'v'}, {'k': 'v'}],
   [{'k': 'v'}, {'k': 'v'}, {'k': 'v'}]]