What is the most "pythonic" way to iterate over a list in chunks?

Answer

Modified from the recipes section of Python's itertools docs:

def grouper(iterable, n, fillvalue=None):
    args =[iter(iterable)]* n
    return izip_longest(*args, fillvalue=fillvalue)

Example
In pseudocode to keep the example terse.

grouper('ABCDEFG',3,'x')-->'ABC''DEF''Gxx'

All python Questions

Ask your interview questions on python

Write Your comment or Questions if you want the answers on python from python Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---