To concatenate lists you can just use the +
operator:
list3 = list1 + list2
If you want to get an iterator over the contents of both lists, you can use itertools.chain()
:
from itertools import chain it = chain(list1, list2)
To concatenate lists you can just use the +
operator:
list3 = list1 + list2
If you want to get an iterator over the contents of both lists, you can use itertools.chain()
:
from itertools import chain it = chain(list1, list2)
This is how to split an iterable into fixed-length chunks or blocks.
If the number of items in the iterable is not a multiple of the block size, the last block is padded with a fill value.
def grouper(iterable, n, fillvalue=None): for i in range(0, len(iterable), n): block = [j for j in iterable[i:i + n]] yield tuple(block + [fillvalue] * (n - len(block)))
itertools.izip_longest()
This is from the itertools
documentation
def grouper(iterable, n, fillvalue=None): "Collect data into fixed-length chunks or blocks" # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args)
alphabet = "".join(chr(c) for c in range(65, 91)) # ABC... print list(grouper(alphabet, 3))
[('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'H', 'I'), ('J', 'K', 'L'), ('M', 'N', 'O'), ('P', 'Q', 'R'), ('S', 'T', 'U'), ('V', 'W', 'X'), ('Y', 'Z', None)]
If you have a list of lists like this:
lol = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
And you want to flatten it into a single list like this:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
You can use a list comprehension like this:
l = [item for sublist in lol for item in sublist]
Or you can use itertools.chain()
like this:
from itertools import chain l = list(chain(*lol))