[Solved] How to replace values in multidimensional array?


string.replace won’t work, since it does not affect the original value.

>>> test = "hallo"
>>> test.replace("a", " ")
'h llo'
>>> test
'hallo'

Instead you need to access the list via the indexes:

for row in ArrayMulti:
    for index in range(len(row)):
        row[index] = "a"

If you provide a more precise question and add the output you want to achieve to the question, I can give you a more precise answer.

I scrapped the previous solution since it wasn’t what you wanted

def UserMultiArray(usrstrng, Itmval):
    ArrayMulti=[[" " for x in range(Itmval)] for x in range(Itmval)]

    for index, char in enumerate(usrstrng):
        ArrayMulti[index//Itmval][index%Itmval] = char
    return ArrayMulti


>>> stack.UserMultiArray("funs", 3)
[['f', 'u', 'n'], ['s', ' ', ' '], [' ', ' ', ' ']]

This little trick uses the whole-number-division:

[0, 1 ,2 ,3 ,4] // 3 -> 0, 0, 0, 1, 1

and the modulo operator(https://en.wikipedia.org/wiki/Modulo_operation):

[0, 1 ,2 ,3 ,4] % 3 -> 0, 1, 2, 0, 1

4

solved How to replace values in multidimensional array?