Some built-in for list entry in python

I have a list of sizes <N and I want to put it up to size N with a value.

Of course, I can use something like the following, but I feel that there must be something that I missed:

>>> N = 5 >>> a = [1] >>> map(lambda x, y: y if x is None else x, a, ['']*N) [1, '', '', '', ''] 
+89
python list list-manipulation
Aug 09 '10 at 9:30
source share
9 answers
 a += [''] * (N - len(a)) 

or if you do not want to change a in place

 new_a = a + [''] * (N - len(a)) 

you can always subclass the list and call the method you like

 class MyList(list): def ljust(self, n, fillvalue=''): return self + [fillvalue] * (n - len(self)) a = MyList(['1']) b = a.ljust(5, '') 
+137
Aug 09 2018-10-09T00:
source share

I think this approach is more visual and pythonic.

 a = (a + N * [''])[:N] 
+22
Oct 07 '16 at 18:18
source share

There is no built-in function for this. But you could make inline functions for your task (or something: p).

(Changed from itertool padnone and take recipes)

 from itertools import chain, repeat, islice def pad_infinite(iterable, padding=None): return chain(iterable, repeat(padding)) def pad(iterable, size, padding=None): return islice(pad_infinite(iterable, padding), size) 

Using:

 >>> list(pad([1,2,3], 7, '')) [1, 2, 3, '', '', '', ''] 
+21
Aug 09 '10 at 10:09
source share

Gnibbler's answer is more pleasant, but if you need the built-in itertools.izip_longest , you can use itertools.izip_longest ( zip_longest in Py3k):

 itertools.izip_longest( xrange( N ), list ) 

which will return a list of tuples ( i, list[ ] ) filled with None. If you need to get rid of the counter, do something like:

 map( itertools.itemgetter( 1 ), itertools.izip_longest( xrange( N ), list ) ) 
+5
Aug 09 2018-10-10T00:
source share

You can also use a simple generator without built-in attachments. But I would not fill out the list, but let the application logic deal with an empty list.

Anyway, an iterator without buildins

 def pad(iterable, padding='.', length=7): ''' >>> iterable = [1,2,3] >>> list(pad(iterable)) [1, 2, 3, '.', '.', '.', '.'] ''' for count, i in enumerate(iterable): yield i while count < length - 1: count += 1 yield padding if __name__ == '__main__': import doctest doctest.testmod() 
+4
Aug 09 '10 at 10:32
source share

If you want to type "No" instead of "", map () does the job:

 >>> map(None,[1,2,3],xrange(7)) [(1, 0), (2, 1), (3, 2), (None, 3), (None, 4), (None, 5), (None, 6)] >>> zip(*map(None,[1,2,3],xrange(7)))[0] (1, 2, 3, None, None, None, None) 
+4
Jan 03 2018-11-11T00:
source share

more-itertools is a library that includes a special padded for this problem:

 import more_itertools as mit list(mit.padded(a, "", N)) # [1, '', '', '', ''] 



Alternatively, more_itertools also implements Python itertools recipes , including padnone and take , as mentioned by @kennytm, so they do not need to be redefined:

 list(mit.take(N, mit.padnone(a))) # [1, None, None, None, None] 

If you want to replace the default padding with None , use a list comprehension:

 ["" if i is None else i for i in mit.take(N, mit.padnone(a))] # [1, '', '', '', ''] 
+3
Aug 22 '17 at 18:38 on
source share

To get out of Kenya:

 def pad(l, size, padding): return l + [padding] * abs((len(l)-size)) >>> l = [1,2,3] >>> pad(l, 7, 0) [1, 2, 3, 0, 0, 0, 0] 
+1
Mar 23 '16 at 23:59
source share
 extra_length = desired_length - len(l) l.extend(value for _ in range(extra_length)) 

This avoids any additional highlighting, unlike any solution that depends on creating and adding a list of [value] * extra_length . The __length_hint__ method first calls __length_hint__ in the iterator and increases the distribution for l by as much as before filling it from the iterator.

+1
Apr 17 '19 at 20:16
source share



All Articles