How to make two-dimensional list in Python (without numpy)?

18,739

Solution 1

The most straightforward way to build it is like this:

list_of_lists = []
for row in range(rows):
    inner_list = []
    for col in range(cols):
        inner_list.append(None)
    list_of_lists.append(inner_list)

or with a list comprehension:

list_of_lists = [[None for col in range(cols)] for row in range(rows)]

The two ways are equivalent.

Solution 2

To create myList initially filled with None, you can do this:

N = 3
myList = [[None] * N for i in range(N)]

print(myList)

Which gives:

[[None, None, None], [None, None, None], [None, None, None]

Then if you want to update each cell, just loop over the rows and columns, where row is the index of the row in the matrix and col is the index of the column in the matrix. Then you can update each my_List[row][col] cell accordingly:

for row in range(N):
    for col in range(N):
        myList[row][col] = row + col

print(myList)

Which Outputs:

[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
Share:
18,739
izza
Author by

izza

Updated on June 15, 2022

Comments

  • izza
    izza about 2 years

    This is probably duplicate question, but I am still curious about this.

    I want to make two-dimensional list in Python without numpy. So I make a list of list. Here is my code:

    myList = [None] * 3
    print('myList :', myList)
    myMatrix = [myList] * 3
    #myMatrix = [[None, None, None], [None, None, None], [None, None, None]]
    print('myMatrix', myMatrix)
    for i in range (0,3):
        for j in range (0, 3):
            myMatrix[i][j] = i+j
        print('myMatrix[',i,'] : ', myMatrix[i])
    
    print(myMatrix)
    print(myMatrix[0])
    print(myMatrix[1])
    print(myMatrix[2])
    

    I know that the statement:

    myMatrix = [myList] * 3
    

    makes the code failed to work as I expected, and it is because list is mutable object, which means myMatrix[0], myMatrix[1], myMatrix[2] will refer to the same object. A change to any of them means a change to all of them, which is not what I expected in my code. Here is the unexpected output of my code:

    ('myList :', [None, None, None])
    ('myMatrix', [[None, None, None], [None, None, None], [None, None, None]])
    ('myMatrix[', 0, '] : ', [0, 1, 2])
    ('myMatrix[', 1, '] : ', [1, 2, 3])
    ('myMatrix[', 2, '] : ', [2, 3, 4])
    [[2, 3, 4], [2, 3, 4], [2, 3, 4]]
    [2, 3, 4]
    [2, 3, 4]
    [2, 3, 4]
    

    The only solution I found is, instead of stating myMatrix = [myList] * 3, I should write:

    myMatrix = [[None, None, None], [None, None, None], [None, None, None]]
    

    That will make the code works as I expected below (the output of the program):

    ('myMatrix', [[None, None, None], [None, None, None], [None, None, None]])
    ('myMatrix[', 0, '] : ', [0, 1, 2])
    ('myMatrix[', 1, '] : ', [1, 2, 3])
    ('myMatrix[', 2, '] : ', [2, 3, 4])
    [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
    [0, 1, 2]
    [1, 2, 3]
    [2, 3, 4]
    

    But it's not an efficient way to define a NxN matrix, especially if N is a big number.

    Does Python have an efficient way to define a NxN matrix using list of list?

    I'm more familiar with C/C++, so this problem is really bugging me. Some answers will recommend me to use numpy, but at this moment I would like to learn basic Python from scratch (without importing any library). When I use C/C++, I can work with this two-dimensional array easily, without importing any library. Asking me to use numpy when I'm just new in Python, is just like asking me to use STL when I'm just new to C.

    Of course I will learn numpy later, but I want to tackle this without numpy first.