zjm1126 zjm1126 - 11 months ago 63
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:
. In other words, if
len(a) < len(b)
, i want to fill up list
with values from list
up to length of the list
, somewhat similar to what

This is my code:

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 Source

Why not just:

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