Concatenate pairs of consecutive sublists in a list using Python -


how combine sublists within list pairs? example with:

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

the result be:

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

you use zip_longest fill value (in case list has odd number of sublists) zip iterator on list1. running list comprehension on zip generator object allows concatenate consecutive pairs of lists:

>>> itertools import zip_longest # izip_longest in python 2.x >>> x = iter(list1) >>> [a+b a, b in zip_longest(x, x, fillvalue=[])] [[1, 2, 3, 4, 5], [6, 7, 8], [9, 10]] 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -