I think that's what you want

nodes = [[Node() for j in range(cols)] for i in range(rows)]

But it is not always a good practice to initialize lists. For matrices it may make sense.

If you're wondering: Documentation about list comprehensions

Demo code:

>>> class Node:
      def __repr__(self):
        return "Node: %s" % id(self)
>>> cols = 3
>>> rows = 4
>>> nodes = [[Node() for j in range(cols)] for i in range(rows)]
>>> from pprint import pprint
>>> pprint(nodes)
[[Node: 41596976, Node: 41597048, Node: 41596904],
 [Node: 41597120, Node: 41597192, Node: 41597336],
 [Node: 41597552, Node: 41597624, Node: 41597696],
 [Node: 41597768, Node: 41597840, Node: 41597912]]
Answer from JBernardo on Stack Overflow
๐ŸŒ
Snakify
snakify.org โ€บ two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
In real-world Often tasks have to store rectangular data table. [say more on this!] Such tables are called matrices or two-dimensional arrays. In Python any table can be represented as a list of lists (a list, where each element is in turn a list).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
Python creates only one inner list and one 0 object, not separate copies. This shared reference behavior is known as shallow copying (aliasing). If we assign the 0th index to another integer say 1, then a new integer object is created with the value of 1 and then the 0th index now points to this new int object as shown below ยท Similarly, when we create a 2d array as "arr = [[0]*cols]*rows" we are essentially extending the above analogy.
Published: December 20, 2025
Discussions

2D array of objects in Python - Stack Overflow
I'm converting some java code to python code and I ended up getting stumped on how to convert a 2D array of objects in Java to python. ... Basically, a list of lists. But it's very likely that there's no reason to start with initializing the list. More on stackoverflow.com
๐ŸŒ stackoverflow.com
2D NumPy array of objects vs. 2D Python list efficiency
I have a 2D list a of varying shape NumPy arrays and equivalent NumPy array of objects b = np.array(a, dtype='object'). It is inconvenient to slice a, e.g. a[:][1] is not equivalent to b[:, 1], canโ€™t be expressed by slicing and requires a list comprehension [e[1] for e in a[:]] instead. More on discuss.python.org
๐ŸŒ discuss.python.org
5
0
March 9, 2023
class - Making a 2d list of objects (Python 2.7) - Stack Overflow
I have no experience with object oriented programming. I've program mostly in C so this tends to be a little confusing. Basically I just want to make sure I am doing it correctly. Also, is this a w... More on stackoverflow.com
๐ŸŒ stackoverflow.com
2D list help in Python
Clear only clears (empties) the current instance. All your rows in your c matrix refer to the same sub_c object that you always clear. You need to create new empty sub_c lists for every single row. More on reddit.com
๐ŸŒ r/learnprogramming
6
1
January 13, 2024
๐ŸŒ
Processing
py.processing.org โ€บ tutorials โ€บ 2dlists
Two-Dimensional Lists \ Tutorials
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
This peculiar functioning is because Python uses shallow lists which we will try to understand. In method 1a, Python doesn't create 5 integer objects but creates only one integer object, and all the indices of the array arr point to the same int object as shown. If we assign the 0th index to another integer say 1, then a new integer object is created with the value of 1 and then the 0th index now points to this new int object as shown below ยท Similarly, when we create a 2d array as "arr = [[0]*cols]*rows" we are essentially extending the above analogy.
Published: June 20, 2024
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
2D NumPy array of objects vs. 2D Python list efficiency - Python Help - Discussions on Python.org
March 9, 2023 - I have a 2D list a of varying shape NumPy arrays and equivalent NumPy array of objects b = np.array(a, dtype='object'). It is inconvenient to slice a, e.g. a[:][1] is not equivalent to b[:, 1], canโ€™t be expressed by slicโ€ฆ
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ 2d-python
Python - 2D List Examples - Dot Net Perls
To construct a 2D list, we can use append() or an initializer. We create an empty list and add empty lists to it with append(). We can construct any rectangular (or jagged) list this way. In this example we build a 2 by 2 list. Step 1 We first create an empty list with the empty square brackets.
Find elsewhere
๐ŸŒ
Beauty and Joy of Computing
bjc.edc.org โ€บ March2019 โ€บ bjc-r โ€บ cur โ€บ programming โ€บ old-labs โ€บ python โ€บ 2D_lists.html
2D Lists in Python
And just like in a 2D cartesian graph, retrieving an element requires two index values (essentially "the x and y position"). >>> produce = ["kale", "spinach", "sprouts"] >>> fruit = ["olives", "tomatoes", "avocado"] >>> cart = [produce, fruit] >>> cart [ ["kale", "spinach", "sprouts"], ["olives", "tomatoes", "avocado"]] The first and second list can be retrieved normally using the known notation.
Top answer
1 of 1
1

Creating the list of lists seems sound. Try it yourself in an interpreter (this about the best advice a new Python programmer can take):

>>> [[None for x in range(20)] for y in range(20)]
[[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]]

When you try to instantiate your AttackRobots, you'll see this:

TypeError: __init__() takes exactly 5 arguments (1 given)

Pass in valid values for team, x, y, and direction (self is implicit) and you should be set!

E.g:

self.arr[x][y] = AttackRobot(team, x, y, direction)
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ 2d list help in python
r/learnprogramming on Reddit: 2D list help in Python
January 13, 2024 -

I'm a little lost on this one, heres the function requirements:

def matrixes_add(a, b):
"""
-------------------------------------------------------
Sums the contents of matrixes a and b. a and b must have
the same number of rows and columns.
a and b must be unchanged.
Use: c = matrixes_add(a, b)
-------------------------------------------------------
Parameters:
    a - a 2D list (2D list of int/float)
    b - a 2D list (2D list of int/float)
Returns:
    c - the matrix sum of a and b (2D list of int/float)
-------------------------------------------------------
"""

Heres an example test and output:

matrixes_add([[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [1, 0]])

--> [[6, 8], [10, 12], [5, 5]]

Heres my code:

    assert len(a) == len(b) and len(a[0]) == len(b[0])

c = []
sub_c = []

for i in range(len(a) - 1):
    for j in range(len(a[i])):
        sub_c.append(a[i][j] + b[i][j])
    c.append(sub_c)
    sub_c.clear()
return c

with the same list as the example i keep getting:

[[], []]

Any suggestions???

๐ŸŒ
Quora
quora.com โ€บ How-do-2D-lists-work-in-Python
How do 2D lists work in Python? - Quora
Answer (1 of 2): You know what a normal list is in Python, right? It's an object that contains other objects in order. It could contain a string. An integer. A boolean. Or even, another list. Another list is how you get a 2d list. A 2d list is a list, where everything it contains is another list...
๐ŸŒ
Cs10
cs10.org โ€บ bjc-r โ€บ cur โ€บ programming โ€บ python โ€บ 2D_lists.html
2D Lists in Python
All of the previously mentioned list functions still operate on 2D lists: >>> cart[0][2] = "cabbage" >>> cart [ ["kale", "spinach", "cabbage"], ["olives", "tomatoes", "avocado"]] Now that you have some experience with lists in Python, try writing a function that takes a list of lists (2D) and returns all the items of each list concatenated together into one new list.
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ get attributes from a 2d list (or array) of objects?
r/Python on Reddit: Get attributes from a 2D list (or array) of objects?
October 3, 2015 -

I have a rather simple question but I haven't been able to find an answer so far: if I have a 2D list of objects, how can I extract all the values from a common attribute?

This is some class:

class Foo():
    def __init__(self, attr):
        self.attr = attr

For a 1D list, it's really simple:

array = [Foo(i) for i in range(6)]
[obj.attr for obj in array]

This will display: [0, 1, 2, 3, 4, 5].

For a multidimensional list, though, this won't work. A solution would be using loops or lists of comprehension to extract the attributes from each column or line. However, slices allow us to access a specific region of a multidimensional list.

So, let's say you have a 6x6 list (or ndarray) of objects, and you want to access the attributes at [4:,4:], or even better, you want to slice the list in a non-contiguous way and then get the attributes of all object elements in the pattern. Is that possible?

Check this image from the numpy docs to have a visual reference about what I'm talking about (see 2nd and 4th slices).

I'm aiming at being able to use this for a board game in which the board would be sliced into different "tile" groups or patterns and then check the properties of the objects in those tiles.

This is not homework.

๐ŸŒ
CodeHS
codehs.com โ€บ tutorial โ€บ evelyn โ€บ 2d-lists-in-python
Tutorial: 2D Lists in Python | CodeHS
Click on one of our programs below to get started coding in the sandbox! ... Learn how to create and manipulate 2D lists!
๐ŸŒ
Du
cs.du.edu โ€บ ~intropython โ€บ intro-to-programming โ€บ 2Dlist_define.html
Defining 2D lists - Introduction to Programming
One way to create a 2D list in python is to use a 2D list literal. You've already learned how to create a 1D list literal, and the syntax here is similar. However, instead of a simple list, here we have a list of lists, or nested list.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ python-2d-array
Python 2D array - Javatpoint
January 10, 2021 - Python 2D array with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.