### Thread: Extending a list till it reaches a given size

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
13
Rep Power
0

#### Extending a list till it reaches a given size

Hi all,

Sorry for the nOOb question but I have been unsuccessfully trying to figure this out which should be simple.

Is there an elegant way to do this:

Code:
```a = [2, 2, 5]
while len(a) < 6:
a.extend([1])```
resulting in a = [2, 2, 5, 1, 1, 1]

I was wondering if there is an awesome python feature that does it in the most beautiful way

Thanks,
Luca
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2007
Location
Joensuu, Finland
Posts
471
Rep Power
71
Originally Posted by pamparana
I was wondering if there is an awesome python feature that does it in the most beautiful way

Code:
```>>> a = [2, 2, 5]
>>> a + [1] * 3
[2, 2, 5, 1, 1, 1]```
(Where the factor 3 could be calculated from the desired length minus the current length.)

• pamparana agrees
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
13
Rep Power
0
Elegant enough! Sold

Many thanks!