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?


Why not just:

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

Use itertools repeat.

>>> from itertools import repeat
>>> a + list(repeat(0, 6))
['a', 'b', 'c', 0, 0, 0, 0, 0, 0]

Why not just

c = (a + b)[:len(b)]

To be more explicit, this solution replaces the first elements of b with all of the elements of a regardless of the values in a or b:

a + b[len(a):]

This will also work with:

>>> a = ['a', ['b'], None, False]
>>> b = [0, 1, 2, 3, 4, 5]
>>> a + b[len(a):]
['a', ['b'], None, False, 4, 5] 

If you do not want to the result to be longer than b:

>>> a = ['a', ['b'], None, False]
>>> b = [0, 1, 2]
>>> (a + b[len(a):])[:len(b)]
['a', ['b'], None]

LIST_LENGTH = 10
a = ['a','b','c']

while len(a) < LIST_LENGTH:
    a.append(0)