how to fill a list with 0 using python

PythonList

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

Why not just:

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

Solution 2 - Python

Use itertools repeat.

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

Solution 3 - Python

Why not just

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

Solution 4 - Python

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]

Solution 5 - Python

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

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

Solution 6 - Python

a+[0]*(len(b) - len(a))

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

Solution 7 - Python

If you want to fill with mutable values, for example with dicts:

map(lambda x: {}, [None] * n)

Where n is the number of elements in the array.

>>> map(lambda x: {}, [None] * 14)
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
>>> l = map(lambda x: {}, [None] * 14)
>>> l[0]
{}
>>> l[0]['bar'] = 'foo'
>>> l
[{'bar': 'foo'}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

Populating without the lambda would cause every element to be {'bar': 'foo'}!

Solution 8 - Python

Can't you just do:

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

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

Solution 9 - Python

Just another solution:

a = ['a', 'b', 'c']
maxlen = 10
result = [(0 if i+1 > len(a) else a[i])  for i in range(maxlen)]

I like some of the other solutions better.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionzjm1126View Question on Stackoverflow
Solution 1 - PythonAchimView Answer on Stackoverflow
Solution 2 - PythonIacksView Answer on Stackoverflow
Solution 3 - PythonletitbeeView Answer on Stackoverflow
Solution 4 - PythondansalmoView Answer on Stackoverflow
Solution 5 - PythonlitepresenceView Answer on Stackoverflow
Solution 6 - PythonjohnnyView Answer on Stackoverflow
Solution 7 - PythonTalosView Answer on Stackoverflow
Solution 8 - PythonprogrammersbookView Answer on Stackoverflow
Solution 9 - PythonDevPlayerView Answer on Stackoverflow