zjm1126 zjm1126 - 2 months ago 7
Python Question

how to fill a list with 0 using python

I want to get a fixed length list from another list like:

a = ['a','b','c']
b = [0,0,0,0,0,0,0,0,0,0]


And I want to get a list like this:
['a','b','c',0,0,0,0,0,0,0]
. In other words, if
len(a) < len(b)
, i want to fill up list
a
with values from list
b
up to length of the list
b
, somewhat similar to what
str.ljust
does.

This is my code:

a=['a','b','c']
b = [0 for i in range(5)]
b = [a[i] for i in b if a[i] else i]

print a


But it shows error:

File "c.py", line 7
b = [a[i] for i in b if a[i] else i]
^
SyntaxError: invalid syntax


What can i do?

Answer

Why not just:

a = a + [0]*(maxLen - len(a))