I want to zero pad a list to a certain length in python, so I can do this:
>>> a = [1,2,3,4]
>>> b = [0,1]
>>> rng = max(len(l) for l in [a,b])
>>> for lst in [a, b]:
... apnd = [0]*(rng-len(lst))
... lst += apnd
...
>>> a
[1, 2, 3, 4]
>>> b
[0, 1, 0, 0]
But lets say I want to prepend that list instead:
>>> a
[1, 2, 3, 4]
>>> b
[0, 1, 0, 0]
>>> a = [1,2,3,4]
>>> b = [0,1]
>>> rng = max(len(l) for l in [a,b])
>>> for lst in [a, b]:
... prpnd = [0]*(rng-len(lst))
... lst = prpnd + lst
...
>>> a
[1, 2, 3, 4]
>>> b
[0, 1]
This doesn't work. Why does the '+=' operator work inside a for loop, but the '=' operator doesn't?
What is a way that I can prepend onto a list inside this for loop?