I am working on a problem that requires that i create a 3x3 matrix in python using the list function. these are the functions that im required to use to create my matrix:
def mlist(size):
list = []
for i in range(size):
list = list + [None]
return list

def mmatrix(rows,cols):
matrix = mlist(rows)
for i in range(rows):
matrix[???] = mlist(???)
return ???

I have modified the functions to the point to where i get this out put:

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

can someone please help me figure out how to make the matrix look right?

Member Avatar for leegeorg07

i used this code:

def mlist(size):
    list = []
    for i in range(size):
        list = list + ['test']
    return list

def mmatrix(rows,cols):
    for i in mlist(rows):
        print(i*cols)

mmatrix(3, 3)

i obviously changed the mmatrix function to an change of an example in python for dummies
i also had to change the item placed in the lists because you cant multiply none by anything. i hope this helps

Here a function that creates a 3xn matrix.

def create_3n_matrix(n):
    return [[[None for x in range(n)] for x in range(n)] for x in range(n)]

calling create_3n_matrix(3) creates the following:

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

It looks like you only want a 2D matrix, since you are talking about rows and columns.

First of all, do not use list as a variable name since list is a builtin function in Python. Lists have a function append(), so use it. Commonly a basic matrix is populated with zeroes, which you then can replace as needed.

Here is a working example:

# create a 2D matrix of zeros and populate it

def make_list(size):
    """create a list of size number of zeros"""
    mylist = []
    for i in range(size):
        mylist.append(0)
    return mylist

def make_matrix(rows, cols):
    """
    create a 2D matrix as a list of rows number of lists
    where the lists are cols in size
    resulting matrix contains zeros
    """
    matrix = []
    for i in range(rows):
        matrix.append(make_list(cols))
    return matrix

mx = make_matrix(3, 3)

print(mx)

print('-'*34)

# now populate the zero matrix
# for instance put a 5 in row 0, column 0
mx[0][0] = 5
# put a 7 in row 1, column 1
mx[1][1] = 7
# put a 9 in row 2, column 2
mx[2][2] = 9

print(mx)

"""
my result -->
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
----------------------------------
[[5, 0, 0], [0, 7, 0], [0, 0, 9]]

if you pretty it up -->
[
[0, 0, 0], 
[0, 0, 0], 
[0, 0, 0]
]
----------------------------------
[
[5, 0, 0], 
[0, 7, 0], 
[0, 0, 9]
]
"""

Thanks for all the help guys.

A None value 3x3 matrix:

[[None]*3]*3
def mlist(size):
    return [0] * size

def mmatrix(rows,cols):
    return [mlist(cols)]*rows

A None value 3x3 matrix:

[[None]*3]*3
def mlist(size):
    return [0] * size

def mmatrix(rows,cols):
    return [mlist(cols)]*rows

This will give you a nasty surprise since you are forming a matrix of alias lists:

mx = [[None]*3]*3

print(mx)

mx[1][1] = 7

print(mx)

"""
my output -->
looks okay
[[None, None, None], [None, None, None], [None, None, None]]
oops!!!!
[[None, 7, None], [None, 7, None], [None, 7, None]]
"""

This will give you a nasty surprise since you are forming a matrix of alias lists:

mx = [[None]*3]*3

print(mx)

mx[1][1] = 7

print(mx)

"""
my output -->
looks okay
[[None, None, None], [None, None, None], [None, None, None]]
oops!!!!
[[None, 7, None], [None, 7, None], [None, 7, None]]
"""

Oops.

Since the OP said use of the list function was a requirement, I'll throw this into the mix:

list(list(0 for i in range(3)) for j in range(3))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.