0

i want to add an element to a multi-list,like this

l = [["0"]*5]*5
print(l)
for i in range(len(l)):
    l[i].append("\n")

but why every child-list in multi-list add 5th '\n'

[['0', '0', '0', '0', '0', '\n', '\n', '\n', '\n', '\n'], ['0', '0', '0', '0', '0', '\n', '\n', '\n', '\n', '\n'], ['0', '0', '0', '0', '0', '\n', '\n', '\n', '\n', '\n'], ['0', '0', '0', '0', '0', '\n', '\n', '\n', '\n', '\n'], ['0', '0', '0', '0', '0', '\n', '\n', '\n', '\n', '\n']]

I want only add an element.

yellow up
  • 19
  • 5

1 Answers1

-2

Your list length is 5. That's why it is appending the 5th \n.

  • Yes but the OP is doing `l[i].append("\n")` so supposedly each sub-list should have ***one*** `\n` which is why the OP is asking... – Tomerikoo Jan 03 '22 at 10:53