Python pad list to be the size of the next multiplier of 6

Viewed 37

I have a list and I want to ensure it's len is a multiplier of 6 and pad with zeros. So if I have: l1 = [1,2,3,4] I will get

[1,2,3,4,0,0]

l2 = [1,2,3,4,5,6,7,8,9]

will be:

[1,2,3,4,5,6,7,8,9,0,0,0]

and [1,2,3,4,5,6] will be:

[1,2,3,4,5,6]

What is the best way to do so?

1 Answers

You can create a list of zeros multiplied by reminder after dividing the length of the list by 6 (it will create the required padding list), then finally add both the lists.

>>> l2 + [0]*(len(l2)%6)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0]
Related