The problem here is that your outer list contains a reference to a single inner list, just repeated. You can see what I mean by taking your resulting test
and reassigning the value of one of the elements:
>>> test[1][0] = 9999
[[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
[9999, 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']]
So even though x
is incrementing, you're still just appending to a single list object because your test
variable is a list of repeated references to the same object.
You can get around this by using a comprehension to initialize your test
variable:
test = [[] for _ in range(len(message))]
You can also use zip
and slicing to get what you want in a single line of code:
[[*z] for z in zip(s[0::2], s[1::2])]