Home>

I thought of the following as a two-dimensional list of pyhton.
t = [['.','.','.'], ['#','#','#']]
I want to replace the value with the comprehension notation, but with SyntaxError: invalid syntax
It will be.

[t [0] [k] = "*" for k in range (len (t [0]))]

Am I doing something basic wrong?

  • Answer # 1

    Substitute in a comprehensionSentenceCannot be placed.
    Please write in a for statement.

    for k in range (len (t [0])):
        t [0] [k] = "*"

  • Answer # 2

    t [0]If you just want to replace, you don't have to use comprehensions to do the following.

    t [0] = ['*'] * len (t [0])

  • Answer # 3

    The comprehension is there to create a new list.
    There's nothing you can't do if you replace t [0] with that new list, but ...

    t [0] = ['*' for k in range (len (t [0]))]